Unlike C++, in Java programming copy constructors are not created by default. The programmer has to code the copy constructor separately in java. Java copy constructor accepts only one argument and this argument must belong to the same type of the constructor. Copy constructor is method where an instance of a class is taken and returned with a new instance which resembles exactly like the class.
In Java programming copy constructors works well along with other constructors. The objects used in Java can be cloned using the clone () method but many programmers feel that copying the object using clone() method is complicated. Clone method of copying is not good because no constructor is called during the cloning of the objects.
- Related: All Java interview Q&A
Let us consider copying an object X and after copying the object X, now we have a second object which resembles the object X but it is not exactly X. if you still have an problem in understanding consider this example: imagine you are playing a game and facing an enemy, if the programmer of this game wants you to face two enemies who look like same; he will copy all the entities of the first and use it for the second enemy. Now you may see two enemies in the environment but they are not same because each will have different characteristics and movements. Like in the above said example the objects copied in Java may resemble same but their characteristics are different.
In general copy constructor is used to create an object which is a copy of another object and takes it as a parameter. Every newly created object is independent from the original object. The two objects are independent from other because they are stored in different address memory.
Why Copy constructors are better than cloning?
Cloning an object is very difficult when it has reference to immutable fields. Cloning an object will result in a copy of the same object with similar characteristics.
How to write a copy constructor?
For writing a copy constructor, let us consider the same video game scenario we discussed above:
Now we want to copy an enemy name Now to create a new enemy called Smith. The copy constructor written to copy the enemies will resemble the following program.
public vinod (vinod copy)
vinod Now = new vinod();
vinod Now = Mokka;
vinod new vinod= new vinod();
// Can copy the new values
newvinod.name = copy.name;
newvinod.bbPoints = copy.bbPoints;
newvinod.gun = copygun;
newvinod.medal = copy.medal;
// Return as copy values
Even Though we have done this using copy constructor both these enemies will resemble same so we have to change the bbpoints or weapon type to experience differences between the enemies. This process of altering the value of the copied object to differentiate to two objects is called as shallow copy.
If we use the process of deep copy major differences can be experienced between the objects. We can’t copy the arrays present in the class instances. Instead every elements needs to be copied otherwise the object will resemble the same instances.
public Myenmy(Myenmy copy)
Myenmy newMyenmy = new Myenmy();
Myenmy Vinod = new Myenmy();
Myenmy Vinod = Mokka;
// Can copy new values
newMyenmy.Hisname = copy.Hisname;
newMyenmy.bbPoints = copy.bbPoints;
newMyenmy.gun = new Gun(copy.gun);
newMyenmy.medal = new Medal();
for (Pool pool: copy.medal)
// return copy
It will get complicated for classes with large trees. So make new instance for everything it is the best way to get deep copies using copy constructors. You may think of using clone method instead of copy constructor but clone method is implemented as a static factory and it is a lousy idea to use it. The main advantage of using copy constructor is that you have complete control over what you are created and it easily returns the required class instance.
For the most part try to avoid copying objects whenever possible because copying instances are very tricky.