Copy Constructor in Java

Last Updated : 16 Jan 2026

Like C++, Java also supports the copy constructor. But in C++, it is created by default. In Java, we define copy constructors ourselves.

Constructor

In Java, a constructor is the same as a method, but the only difference is that the constructor has the same name as the class name. It is used to create an instance of the class. It is called automatically when we create an object of the class. It has no return type. Remember that a constructor cannot be abstract, final, synchronized, or static. We cannot override a constructor. It occupies memory space when it is called.

To read more Constructor in Java

Copy Constructor

In Java, a copy constructor is a special type of constructor that creates a new object using an existing object of the same class. It returns a duplicate copy of an existing object of the class.

We can assign a value to the final field, but this cannot be done when using the clone() method. It is used if we want to create a deep copy of an existing object. It is easier to implement than the clone() method.

The primary use case of a copy constructor is to create a new object with the same state as an existing object. It is particularly useful in scenarios where we need to duplicate an object while maintaining its integrity. By leveraging a copy constructor, we ensure that the new object is independent of the original one, thereby preventing unintended side effects that might occur with shallow copying.

Note: The subclasses cannot inherit it. If we attempt to initialize a child class object from a parent class reference, we encounter a casting problem when cloning it with the copy constructor.

Usage of Copy Constructor

We can use the copy constructor if we want to:

  • Create a copy of an object that has multiple fields.
  • Generate a deep copy of the heavy objects.
  • Avoid the use of the clone() method.

Creating a Copy Constructor

To create a copy constructor in Java, follow the steps given below:

  • Create a constructor that accepts an object of the same class as a parameter.
  • Copy each field (variable) object into the newly created instance.

Example of Copy Constructor

Example

Compile and Run

Output:

Name: Peter, Age: 22
Name: Peter, Age: 22

In the above program, s2 is a new object that copies the values from s1 using the copy constructor. It's especially useful in cases where you want to duplicate an object without affecting the original.

Copy Constructor Vs. clone() Method

Both the copy constructor and the clone() method are used to create a copy of an existing object of the class. But the use of the copy constructor is easier and better in comparison to the clone() method because of the reasons given below:

  • If we are using the clone() method, it is necessary to import the Cloneable. The method may throw the exception CloneNotSupportedException. Therefore, handling exceptions in a program is a complex task. While in the copy constructor, there are no such complexities.
  • We cannot assign a value if the fields are final. While in the copy constructor, we can assign values to the final fields.
  • The object returned by the clone() method must be typecast. While in the copy constructor, there is no such requirement.

Example: Deep Copying Using Copy Constructor and Cloneable Interface

Example

Compile and Run

Output:

Employee Name: Alice, City: Los Angeles, Country: USA
Employee Name: Alice, City: New York, Country: USA

Explanation

The above Java program demonstrates deep copying in Java using a copy constructor and the Cloneable interface. The Address class overrides the clone() method using super.clone(), which performs a shallow copy-but since Address only holds String fields (which are immutable), it works safely.

  • In the Employee class, we wrote a copy constructor that calls the clone() method on the Address object, enabling a deep copy.
  • As a result, emp1 and emp2 end up with separate Address objects. So, changing the city in emp1 doesn't affect emp2, which confirms the deep copy behavior.

Here are a few more points to consider regarding Java copy constructors:

Immutable Objects: Copy constructors are often used in classes that represent immutable objects. Immutable objects are objects whose state cannot be modified after they are created. By providing a copy constructor, immutable classes enable clients to create new instances with the same state as existing ones, thereby eliminating the risk of inadvertent modification.

Defensive Copying: In scenarios where classes encapsulate mutable objects or collections, copy constructors play a crucial role in defensive copying. Defensive copying involves creating copies of mutable fields before returning them or passing them to other methods. It prevents external code from unintentionally modifying the object's internal state.

Performance Considerations: While copy constructors provide a convenient way to create independent copies of objects, developers should be mindful of their performance implications, especially when dealing with large or deeply nested objects. Deep copying, in particular, can be resource-intensive, as it involves recursively copying all nested objects and their dependencies.

Cloning: In addition to copy constructors, Java provides the concept of object cloning through the Cloneable interface and the clone() method. While cloning achieves a similar outcome of creating a copy of an object, it has its own set of considerations and limitations, such as the need to handle checked exceptions and the default shallow copying behaviour.

Copy Constructors in Inheritance: When dealing with inheritance hierarchies, subclasses can override the copy constructor of their superclass to include additional fields or customize the copying process. It allows subclasses to inherit the behavior of the superclass's copy constructor while extending it to suit their specific requirements.

Serialization and Deserialization: Copy constructors can be useful in the context of serialization and deserialization, where objects are converted into a byte stream for storage or transmission and then reconstructed into their original form. By providing a copy constructor, classes can implement custom deserialization logic to restore objects from serialised data efficiently.

Encapsulation and Information Hiding: Copy constructors can help maintain the principles of encapsulation and information hiding in object-oriented design. By encapsulating the copying logic within the class itself, the internal structure and representation of objects are shielded from external manipulation, promoting a cleaner and more maintainable codebase.

Consistency and Predictability: Copy constructors help to ensure consistency and predictability in object creation and manipulation. Unlike ad-hoc copying mechanisms, where copying logic may vary across different parts of the codebase, using a standardized copy constructor promotes uniformity and reduces the likelihood of errors or inconsistencies.

Cloning Complex Data Structures: Copy constructors are particularly beneficial when dealing with complex data structures, such as trees, graphs, or networks, where maintaining the integrity of relationships between objects is critical. By recursively copying each component of the data structure, copy constructors facilitate the creation of deep copies that faithfully replicate the original structure.

Deep versus Shallow Copying: Copy constructors enable developers to select between deep and shallow copying strategies according to the application's requirements. While shallow copying may suffice for simple objects with primitive fields, deep copying is essential for preserving the integrity of complex objects with references to mutable states.

Custom Copying Logic: In certain scenarios, classes may require custom copying logic beyond a straightforward field-by-field replication. Copy constructors provide the flexibility to implement such custom copying logic tailored to the specific needs of the class, such as performing validation checks, applying transformations, or handling special cases during copying.

Copy Constructors and Design Patterns: Copy constructors are aligned with several design patterns, such as the Prototype pattern, which focuses on creating new objects by copying existing ones. By leveraging copy constructors in conjunction with design patterns, developers can achieve cleaner, more modular, and reusable code designs.

Conclusion

In conclusion, the Java copy constructor is a powerful tool for creating independent copies of objects, facilitating the replication of object states while ensuring data integrity and encapsulation. Its versatility allows developers to manage complex data structures, implement defensive coping strategies, and maintain consistency and predictability in object-oriented designs.

By understanding the significance and practical applications of copy constructors, developers can enhance code maintainability, promote reusability, and achieve more robust software solutions in Java programming.