Nov 8, 2018

Copy constructor in Java

A copy constructor is a constructor that takes only one parameter which is the same exact type as the class in which the copy constructor is defined. For example, suppose we define a class called I88ca. Then, a copy constructor would also expect a parameter of type I88ca.

private I88ca i88ca;

//copy constructor
public I88ca getI88ca()
   return new I88ca(i88ca);

Copy constructor is used so that the other people cannot get the reference to the original Object and modify it through that reference. If your Object is immutable such as String or final then you don't need to worry about it.