Copy Constructor in java

In this post, we will see about copy constructor in java.

Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy constructor in your class.Copy constructor is alternate to clone method.

Let’s say you have class name Country, so copy constructor will look like below:

Java Copy Contructor

Copy constructors are used to create duplicate objects or cloned objects.Newly created object will have same characteristics as the original object. We need to be very careful while using Copy constructor because it is the responsibility of programmer to ensure that newly created object refers to different memory location than original. If you are confused here, don’t worry, I will explain you this with the help of an example.

Let’s understand use of Copy constructor with the help of simple example.
Create a class name capital.java as below:

Create another class as Country.java. Please note that Country has a reference to above capital class.

Let’s create main class named "CopyConstructorMain.java"

When you run above program, you will get below output:

Country name of Copied object: India
Country population of Copied object: 90000
Capital name of Copied object: Delhi
Capital population of Copied object: 10000
============================================
Capital name of Original object: Mumbai
Capital name of Copied object: Mumbai

We are able to copy all content of countryIndia to countryIndiaCopied. That’s great!!
But did you notice an issue here? When we changed capital name in original object, it got changed in duplicate object too.This is because we have done shallow copy in case of capital object, so both objects are referring to same capital reference.You need to be careful while using copy constructor as it may result in unexpected behavior and it is very hard to debug.

Let’s create a deep copy in copy constructor of the Country class and it should fix above issue.
Introduce a copy constructor in Capital class and use it in copy constructor of the Country class.
Capital.java

Country.java

Now you execute CopyConstructorMain.java, you will get below output:

Country name of Copied object: India
Country population of Copied object: 90000
Capital name of Copied object: Delhi
Capital population of Copied object: 10000
============================================
Capital name of Original object: Mumbai
Capital name of Copied object: Delhi

As you can see here, when we made changes to capital name of original object, it did not impact copied object.

If you have mutable object in the class then you need to take care of it by doing deep copy while creating duplicates.Otherwise, it may result in unexpected behaviour.

that’s all about copy constructor in java.

Was this post helpful?

Leave a Reply

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