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

Student(Student s) { // copy constructor code; }

A copy constructor simply takes a single argument whose type is class containing constructor. In other words, it simply takes one parameter as a reference to an object of the same class.



Let’s see a simple example program step by step to understand the concept.

Java Copy Constructor Example Programs

Program source code 1:

package copyConstructor; // Step 1: Create a class Number. public class Number { // Step 2: Declare instance variables a and b with data type int. int a = 10; int b = 20; // Step 3: Declare a default constructor. public Number() { } // Step 8: Declare a copy constructor. public Number(Number n) { a = n.a; b = n.b; } public static void main(String[] args) { // Step 4: Create an object of the class Number and print the value by calling variable a and b using reference variable. // We cannot directly call non-static member in the static region. Number n = new Number(); System.out.println("Value of a: " +n.a); System.out.println("Value of b: " +n.b); // Step 5: Now we are updating values of a and b in the existing object i.e existing memory location. n.a = 50; n.b = 40; System.out.println("Updating values of a and b in the existing object"); System.out.println("Value of a: " +n.a); // 30 System.out.println("Value of b: " +n.b); // 40 // Step 6: But when we create a new object, we will not get updated values of a and b in the new object. Take a look. Number n2 = new Number(); System.out.println("Not getting updated values of a and b in the new object"); System.out.println("Value of a: " +n2.a); // 10 // Getting initial data. System.out.println("Value of b: " +n2.b); // 20 // Getting initial data. // Step 7: To get the updated values of a and b, create another new object and pass the reference variable n of an existing object. Number n3 = new Number(n); // It will call a copy constructor defined in the class. System.out.println("Getting updated values of a and b in the new object"); System.out.println("Value of a: " +n3.a); // 50. Now we are getting updated values of a and b. System.out.println("Value of b: " +n3.b); // 40 } } Output: Value of a: 10 Value of b: 20 Updating values of a and b through reference variable n Value of a: 50 Value of b: 40 Not getting updated values of a and b in the new object Value of a: 10 Value of b: 20 Getting updated values of a and b in the new object Value of a: 50 Value of b: 40 Memory Allocation in Copy constructor concept Consider the above program to understand memory allocation in copy constructor concept. 1. In the main method , when we create an object of the class, Internally, JVM allocates memory for it and initialized the data 10 and 20 inside the memory after replacing 0 in the loading phase. Now we called the variables a and b using reference variable n, we got the output 10 and 20. 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. Key points:

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:

package copyConstructor; public class Mango { boolean sweet; Mango(boolean b) { sweet = b; } Mango(Mango m) { sweet = m.sweet; } public void flavor() { if(sweet) System.out.println("Mango is sweet"); else System.out.println("Mango is not sweet"); } } public class FlavorTest { public static void main(String[] args) { Mango m1 = new Mango(true); Mango m2 = new Mango(m1); m1.flavor(); m1.sweet = false; m2.flavor(); m1.flavor(); } }

Output: Mango is sweet Mango is sweet Mango is not sweet1.

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.

2. It can be used to copy data from one memory location to another memory location in Java.