Copy Constructor in Java

What is Copy Constructor

Sometimes a programmer wants to create an exact but separate copy of an existing object so that subsequent changes to the copy should not alter the original or vice versa. This is made possible using the copy constructor.

A copy constructor is a constructor that creates a new object using an existing object of the same class. Copy constructor takes single argument which is of the type as the class itself in which the copy constructor is implemented.

For example, let us assume a class namely User and it has a constructor called copy constructor which expects only one argument of type User. Continue reading “Copy Constructor in Java”