Copy Constructor in Java

In this article, we will see java copy constructor, its uses, advantages and example. Let’s start!!!

Why do we need a copy constructor in java?

Knowing that Java provides a special functionality like a copy constructor, we come to the question, “What makes copy constructor important in Java?”. Generally, when we create an exact copy of an object belonging to the same class, it results in some discrepancies. We must make sure that any change made should reflect in the original value and vice versa. Here is where the copy constructor lends a helping hand.

More about copy constructor in java:

To be precise, a copy constructor lets the programmer create an object using another object of the same class. In C language, a copy constructor would be created in a default manner. But this is not the case in Java; therefore, you must know the steps to do so. A new object can be created by initializing it with another object of the same class.

Uses of java copy constructor:

  • We can create an object’s copy with various fields.
  • Copy constructors let us create a deep copy of heavy objects.
  • It can replace the usage of the clone() method.

Advantages of copy constructor method in java:

  • It lets us change the fields that are declared as final.
  • The need for typecasting is absent.
  • If the class contains an object with several fields, copy constructors would be of great help.
  • Adding extra fields to the class becomes easy just by changing the input to the constructor.
  • Complete control over the creation of an object can be achieved.

Sample program to create a copy constructor in Java:

public class FirstCode{
private int id;
private String course;
public FirstCode(FirstCode fc) // copy constructor
{
}
}

Now, we can copy each variable object into a newly created object.

public class FirstCode{
private int id;
private String course;
public FirstCode(FirstCode fc) // copy constructor 
{
this.id = fc.id;
this.course = this.course;
}
}

Sample program to demonstrate copy constructor in Java:

public class FirstCode{
private int id;
private String course;
FirstCode(int id2, String course2){
id = id2;
course = course2;
}
FirstCode(FirstCode fc){
System.out.println(“Copy Constructor is invoked”);
id = fc.id;
course = fc.course;
}
int showId(){
return id;
}
String showCourse(){
return course;
}

public static void main(String args[]){
FirstCode fc1 =new FirstCode(123, “Java”);
System.out.println(“The Course ID is: ”+fc1.showId());
System.out.println(“The Course Name is: ”+fc1.showName());
FirstCode fc2 = new FirstCode(fc1);
System.out.println(“Another Course ID: ”+fc2.showId());
System.out.println(“Another Course Name: ”+fc2.showName());
}
}

Output:

The Course ID is: 123
The Course Name is: Java
Copy Constructor is invoked
Another Course ID is: 123
Another Course Name is: Java

Java copy constructor and the clone() method:

Java lets us use the copy constructor or clone() method to create a copy of an existing object. Comparatively, the copy constructor works finer than the clone() method. The reasons for this issue are:

1. The usage of the clone() method demands the importing of the Cloneable interface. At times, it also throws exceptions like CloneNotSupportException. And as we already know, exceptions are always hectic to handle. On the other side, we see no such complexities in using the copy constructor.

2. When the fields are declared final, there is no possibility to assign values in case 1. Whereas, in case 2, we can assign values to the final fields. The object that the clone() method returns must be typecase, and there is no such rule for copy constructor.

Summary

This was all about copy constructor in java. Hope you liked it and enjoyed the article.

Leave a Reply

Your email address will not be published. Required fields are marked *