Copy Constructor in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

In Java, a copy constructor creates an exact duplicate of an object, unlike the default behaviour in languages like C++. Essential for complex objects, it initializes new instances with the same values as existing ones, facilitating deep copies and value swapping without affecting the original object's state.

Note: Java doesn't implicitly provide the facility of a Copy constructor like C language.

Writing a Copy Constructor

A java copy constructor returns a copy of the specified object by taking the existing object as an argument.

To create a copy constructor, we need to take the existing object as an argument and initialize the values of instance variables with the values obtained in the object.

For example,

The above code snippet will work fine if we use immutable and primitive data types (int and String) but what if we have mutable data types in the class?

If a class has mutable data types, we can create a deep copy instead of a shallow copy such that the newly created object becomes independent of the existing object, as shown in the example below.

Example

Now that we have understood how the copy constructor is written in Java. Let us see an example where we create a class Student to hold information about a student and use a copy constructor to create an exact copy from an existing student.

Output

Here, we are invoking the copy constructor by creating a new Student object and passing the existing object in the copy constructor as an argument,

From the Output, we can see that the contents of both (existing and copied object) are exactly the same.

Advantages of Copy Constructor in Java

Using a Copy constructor in Java has several advantages that encourage programmers to use them in the code. Let us discuss these advantages:

  • Copy constructor reduces complexities when we deal with objects with several parameters.
  • If a new field is added to the class, we need only to change the input parameter in the constructor.
  • One of the most important advantages of using a copy constructor is that there is no need for any typecasting.
  • Copy constructor in Java allows us to change the fields that are declared as final.
  • Copy constructor provides complete control over object creation.

Creating Duplicate Objects without Using a Copy Constructor

It is possible to create a duplicate object without using a copy constructor and by simply assigning the values of one object to the other object. Let us create a duplicate student using the copy constructor from the example shown in the article.

Output

Here a duplicate object (student_copy) is created without using a copy constructor. Values of student are simply assigned to student_copy.

Copy Constructor vs. clone() Method

Copy Constructor vs clone method

Instead of using the copy constructor, we can use the clone() method to create a copy of an object, but it is preferable to use the copy constructor mainly because of the following reasons:

Copy constructorclone() method
Copy constructor in Java allows us to change the fields that are declared as final.When we use the clone() method, we cannot assign a value to final fields.
Object is returned in the required type, and there is no need to typecast the returned object.Returned object from clone() method needs to be typecasted to the appropriate type.
Comparatively easier to use and implement the copy constructor as it does not require implementing any additional interface.clone() method requires implementing the Cloneable interface in our program and also needs to handle the CloneNotSupportedException.

Conclusion

  • Java copy constructor is used to create a copy of an existing object of the same class.
  • Copy constructor can be created by explicitly passing the object as an argument of the same class whose object we want to create.
  • Copy of an object can also be created without using a copy constructor and by simply assigning the values of one object to the other object or using the clone() method.
  • Using copy constructor provides simplicity and several advantages over using the clone() method or manually assigning the values.