Copy Constructor in Java

Home / Copy Constructor in Java

Copy constructor in java is used to create the copy of an object.

Is there any copy constructor in java by default?

Java does not create any copy constructor by default like c++, We have to create the copy constructor explicitly.

First look at the complete code of copy constructor than we explain it step by step.

package Practice; 

  class Student { 
	private int id; 
	private String name; 

	public Student(Student copy) { 
		this.id = copy.id; 
		this.name = copy.name; 
	} 

	public Student(int id, String name) { 
		this.id = id; 
		this.name = name; 
	} 

	@Override 
	public String toString() { 
		return "Student [id=" + id + ", name=" + name + "]"; 
	} 
} 

public class CopyConstructor { 
	public static void main(String[] args) { 
		Student s1 = new Student(1, "vasu"); 
		Student s2 = new Student(1, "vishu"); 

		Student s3 = new Student(s1); 

		System.out.println(s1); 
		System.out.println(s2); 
		System.out.println(s3); 
	} 

} 

Now Run this programe and see the output.

Student [id=1, name=vasu] 
Student [id=2, name=vishu] 

Student [id=1, name=vasu] 

Explanation of above code:

To create a Copy constructor we have to define our class constructor like below.

class Student { 
 private int id; 
 private String name; 

public Student(Student copy) { 
      this.id = copy.id; 
      this.name = copy.name; 
  } 
}

In above syntax we define a Student class with two fields

  1. Id
  2. name

And a constructor that accept Student class object. And we assign this Student object value to our instance variable.

Now create a parameterized constructor and override the toString() method to see how the copy constructor works. Now our code looks like below:

class Student { 
	private int id; 
	private String name; 

	public Student(Student copy) { 
		this.id = copy.id; 
		this.name = copy.name; 
	} 

	public Student(int id, String name) { 
		this.id = id; 
		this.name = name; 
	} 

	@Override 
	public String toString() { 
		return "Student [id=" + id + ", name=" + name + "]"; 
	} 
} 

As you can see we have a class named Student with two constructor.

  1. Parameterized constructor
  2. Copy Constructor

Parameterized constructor accept id and name as a parameter and assign them to our instance variable.

Copy Constructor accepts a Student object and assigns them to our instance variable.

Now create the main method and create Object of Student class like below.
public static void main(String[] args) { 
		Student s1 = new Student(1, "vasu"); 
		Student s2 = new Student(1, "vishu"); 

		Student s3 = new Student(s1); 
		System.out.println(s1); 
		System.out.println(s2); 
		System.out.println(s3); 
	} 

As you can see we have define two object of class Student s1 and s2.

Now we create another Student object named s3 and assign object s1 to it.

Now we simply print all three objects.

See the result of the s3 object it is the same as the s1 object. This is because of the copy constructor because we pass this s1 object to our copy constructor using the below line of code.

Student s3 = new Student(s1); 

Copy constructor create the shallow copy not deep copy of objects.

Now you are confused why we use a copy constructor if we create the copy of the object using the clone method.

Copy Constructor Vs Clone method.

  1. It is very easy to create a copy of an object using a copy constructor as compare to the Clone method.
  2. If we use the Clone method then we have to implement the Clonable interface and also need to handle CloneNotSupportedException.

You may also like :

Spring boot security using JWT ( JSON WEB TOKENS ).

Optional in java 8 complete example

Help Others, Please Share

About Author

Leave a Reply

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

x