Copy Constructor in Java
A constructor which is used to copy the data of one object to another object of the same class type is called copy constructor in Java. It is called when a single object reference of the same class is passed as an argument. By default, Java compiler does not create any copy constructor in the class like C++. Like a constructor, a copy constructor also does not have a return type.
There are two statements given below.
Student s1=new Student();
Student s2=new Student(s1);
The second statement makes a call to copy constructor defined in the class. In the above two statements, we have created two objects with reference variables s1 and s2. If we write s2=s1 then it means that s2 is an object reference variable which is created the same as the object reference variable s1 and both s1 and s2 are pointing to the same object. No content is copied.
But when we create Student s2=new Student(s1); it means that an object reference variable s2 is created and it is pointing to a Student object created through the new operator which will call to a copy constructor to copy contents from the first object. For a class Student, a copy constructor can be declared as follows.
// copy constructor code;
Let’s see a simple example program step by step to understand the concept.
Java Copy Constructor Example Programs
Program source code 1:
Memory Allocation in Copy constructor concept
2. In step 5, we have updated the values of a and b by using the reference variable n, In the existing memory, the values of a and b are replaced with 50 and 40 respectively as shown in the memory.
3. In step 6, when we created a new object of the class, a new memory is allocated by JVM but we did not get updated values in the new memory as shown in the above figure because JVM did not copy the updated data into the new memory. So, what will we do to copy the data from one memory to another memory when we create a new object?
4. To copy the data from one memory to another memory, we will use a copy constructor concept. In step 7 of the program, we have created another new object with a reference variable n3 and passed n as an argument to call copy constructor defined in the class. Now, you will get updated data inside the new memory location with the help of a copy constructor. Thus, copy constructor works.
1. So, we can say that a copy constructor is a constructor which is used for copying the data from one memory location (object) to another memory location (new object) meanwhile object creation.
2. Java always passes objects as the reference, but the references are passed by value.
Program source code 2:
1. In the main when we created the first object of the class and passed true as an argument, the sweet boolean data members get the signed value of m1. When we called the flavor() method for m1, the result displays “Mango is sweet”.
2. When we created the second object with reference variable m2 and passed m1 as an argument, the copy constructor is called. As sweet was having true value for m1, m2’s sweet will be having true value too. If we call flavor for m2, the result shows “Mango is sweet”
3. Next, when we have changed the value of sweet for object m1 from true to false and called flavor() method for m1, it shows the result as “Mango is not sweet”. Thus, our copy constructor has worked.
Uses of Copy constructor in Java
1. A copy constructor is used to declare and initialize an object from another object.
Hope that this article has covered all the important concept related to a copy constructor in Java with the practical example programs. We hope that you will have enjoyed this article.
Thanks for reading!