Understanding the copy constructor in java is essential for any Java developer working with object-oriented programming. A java copy constructor example shows how to create an independent duplicate of an existing object – controlling exactly which fields are copied and how nested objects are handled.
This guide covers all java constructor types, the complete copy constructor in java with example program, the crucial difference in shallow copy vs deep copy in java, java object cloning via the clone() method, and every approach to how to copy an object in java.
Java Constructor Types – A Quick Overview
Java constructor types determine how objects are initialised when they are first created. Java supports four primary java constructor types, each serving a distinct purpose. Understanding all four is the foundation for mastering the copy constructor.
Constructor Type |
Description |
When Used |
Default Constructor |
No-argument constructor provided by Java if none is defined – initialises fields to default values (0, null, false) |
When no initialisation arguments are needed |
Parameterised Constructor |
Accepts arguments to initialise fields with specific values at creation time |
When objects need custom initial state |
Copy Constructor |
Creates a new object by copying state from an existing object of the same class |
When an independent duplicate of an object is needed |
Private Constructor |
Prevents external instantiation – used in Singleton and static utility classes |
Design patterns: Singleton, Factory, Builder |
Among all java constructor types, the copy constructor is the most nuanced – it requires understanding object references, memory layout, and the difference between shallow and deep copying to implement correctly.

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
What is a Copy Constructor in Java?
A copy constructor in java is a special constructor that creates a new object by copying the field values from an existing object of the same class. It accepts an instance of the same class as its only parameter and initialises the new object with identical (or customised) state.
Property |
Value |
Parameter |
Single argument – an object of the same class |
Purpose |
Create an independent copy of an existing object |
Inheritance |
Cannot be inherited – must be defined in each class/subclass |
Access |
Can access all fields (public, protected, private) of the passed object |
Default provided? |
No – Java does not auto-generate a copy constructor (unlike C++) |
Primary use case |
Deep copy in java – ensuring the copy is fully independent of the original |
Key Concept: Java does NOT provide a copy constructor automatically. Unlike C++, which generates one by default, Java requires you to write it explicitly. This gives you full control over how copying behaves – especially critical for deep copy in java scenarios. |
Basic Copy Constructor in Java – Example
|
Types of Copy Constructors in Java
The two types of copy constructor in java – shallow and deep – differ in how they handle reference-type fields (arrays, objects nested inside the class).
1. Shallow Copy (Default Copy) Constructor
A shallow copy duplicates primitive fields by value but copies object reference fields by reference – meaning both the original and the copy point to the same nested objects in memory. Modifying a mutable nested object through the copy will also change the original.
|
Key Concept: Shallow copy works correctly for primitive fields (int, double, boolean) and immutable objects (String – already immutable). It only causes problems with mutable reference types like arrays, ArrayLists, and custom objects. |
2. Deep Copy Constructor
Deep copy in java creates completely independent copies of all fields – including nested objects and arrays. Modifying the copy never affects the original. This is the correct approach for mutable reference fields.
|
Shallow Copy vs Deep Copy in Java
Shallow copy vs deep copy in java is one of the most critical distinctions in Java object handling. Choosing the wrong approach leads to subtle, hard-to-debug bugs where modifying one object unexpectedly changes another.
Dimension |
Shallow Copy |
Deep Copy in Java |
Primitive fields |
Copied by value – fully independent |
Copied by value – fully independent |
Immutable fields (String) |
Reference copied – safe (immutable objects can’t change) |
Reference copied – also safe |
Mutable reference fields |
Reference copied – SHARED between original and copy |
New object created – INDEPENDENT |
Arrays |
Reference to same array – dangerous |
Array cloned or new array created – safe |
Nested objects |
Same nested object reference shared |
Each nested object deep-copied recursively |
Memory usage |
Lower – fewer new objects created |
Higher – all nested objects duplicated |
Performance |
Faster – no recursive copying |
Slower – recursive duplication |
Safety |
Unsafe for mutable fields |
Safe – modifications fully isolated |
Java implementations |
p1 = p2 (reference copy), Object.clone() default |
Copy constructor (manual), serialisation trick |
Side-by-Side: Shallow vs Deep Copy Behaviour
|

82.9%
of professionals don't believe their degree can help them get ahead at work.
How to Copy an Object in Java – All Methods
How to copy an object in java: Java provides several mechanisms, each with different trade-offs in safety, effort, and performance. Understanding all approaches is essential for choosing the right one for a given situation.
Method |
Mechanism |
Copy Type |
Effort |
Best For |
Copy Constructor |
Define a constructor taking same-class object |
Deep (manual) |
High |
Full control; recommended approach |
Object.clone() |
Implement Cloneable, call super.clone() |
Shallow by default |
Medium |
Simple classes without mutable fields |
Serialisation Trick |
Serialise then deserialise |
Deep (automatic) |
Low code, high overhead |
Quick deep copy without writing constructors |
Manual Field Copy |
Assign each field individually |
Deep (manual) |
High |
One-off copies in specific methods |
Copy Factory Method |
Static factory returning a new copy |
Deep (manual) |
Medium |
Preferred over clone() by Joshua Bloch |
Method 1: Copy Constructor (Recommended)
Already covered in detail above – the recommended approach for
java copy object: full control, no interface required, works with all field types.
Method 2: Java Object Cloning via clone()
|
Method 3: Serialisation Deep Copy
// how to copy an object in java - serialisation trick
import java.io.*;
public class SerialCopy {
@SuppressWarnings("unchecked")
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
return (T) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException("Deep copy failed", e);
}
}
}
// Usage: Person copy = SerialCopy.deepCopy(original);
// Works automatically for all Serializable classes - no manual field copying
Method 4: Copy Factory Method (Joshua Bloch’s Recommendation)
|
Copy Constructor in Java with Example Program – Extended
The following copy constructor in java with example program demonstrates a production-realistic scenario – a Student class with nested Address and an array of Grade objects, requiring a complete deep copy in java implementation.
|
Java Object Cloning - The clone() Method
Java object cloning via the clone() method is the alternative to the copy constructor. To use it, a class must implement the Cloneable interface and override clone() from Object. If Cloneable is not implemented, calling clone() throws CloneNotSupportedException.
Property |
clone() Method |
Interface required |
Must implement Cloneable marker interface |
Default behaviour |
Shallow copy - super.clone() copies references |
Deep copy |
Requires manual override for each mutable field |
Exception |
Throws CloneNotSupportedException - must be handled |
Return type |
Returns Object - requires explicit casting |
Inheritance |
clone() is inherited from Object - available to all classes |
Recommendation |
Joshua Bloch (Effective Java) advises against it - use copy constructors or factories instead |
Comparison: Copy Constructor vs clone() Method
Dimension |
Copy Constructor |
clone() Method |
Definition |
Special constructor taking same-class object |
Method from Object - overridden with Cloneable |
Interface required |
None - no marker interface needed |
Must implement Cloneable |
Default copy type |
Deep copy (manual - full control) |
Shallow copy by default |
Deep copy support |
Full - explicit field-by-field control |
Requires manual override per mutable field |
Exception handling |
No checked exceptions |
Throws CloneNotSupportedException |
Return type |
New object of same type - no casting |
Returns Object - requires explicit cast |
Field access |
Can access public, protected, and private fields |
Can access public and protected only via super.clone() |
Inheritance |
Cannot be inherited - must define in each subclass |
Inherited from Object - available universally |
Performance |
More efficient - direct member access |
Slightly less efficient - method call + casting |
Recommendation |
Preferred - explicit, safe, flexible |
Use with caution - error-prone for complex objects |
Advantages & Disadvantages of Copy Constructor in Java
Advantage |
Disadvantage |
Full control over copying - including deep copy in java |
Must be manually implemented in each class and subclass |
No interface required - unlike Cloneable |
Cannot be inherited - must be re-defined per subclass |
Accesses all fields (public, protected, private) |
Does not support serialisation directly |
Safe object initialisation - prevents shared mutable state |
More code than assignment (p1 = p2) for simple objects |
Flexible - customise copy behaviour per class |
Risk of implementation errors (forgetting to copy a field) |
Applications of Copy Constructor in Java
• Deep Copy of Complex Objects: When an object contains mutable nested objects (lists, arrays, other objects), the copy constructor ensures the duplicate is fully independent - preventing unintended shared state.
• Immutable Object Design: Copy constructors are used to create defensive copies of mutable parameters passed into constructors, ensuring the immutable object's internal state cannot be modified externally.
• Object Caching & Prototyping: The Prototype design pattern uses copy constructors to clone pre-configured objects - creating new instances from a cached prototype rather than initialising from scratch.
• Undo/Redo Systems: Applications like text editors snapshot object state using copy constructors - each edit creates a deep copy of the current state to enable undo.
• Thread Safety: Creating deep copies of shared mutable objects before passing them to different threads prevents race conditions and data corruption.
• Builder Pattern: Copy constructors are used to create a working copy of an object before applying incremental modifications through a builder, then discarding the copy if any step fails.
Conclusion
The copy constructor in java is the most controlled, flexible, and recommended way to java copy object - especially when deep copying is required. Understanding shallow copy vs deep copy in java is critical: always use deep copy when your class contains mutable reference fields (arrays, collections, nested objects) to ensure the copy is fully independent.
The copy constructor in java with example program in this guide demonstrates both patterns - from the basic Student example to the production-realistic multi-level deep copy. Compared to java object cloning via clone(), the copy constructor is safer, more explicit, and more maintainable.
To master Java OOP patterns and build production-grade applications, explore the Certificate Program in Application Development powered by Hero Vired.
People Also Ask
What is a copy constructor in Java?
Copy constructor in java: a special constructor that takes an object of the same class as its argument and creates a new independent object with the same field values. Java does not provide one automatically - it must be explicitly defined. The copy constructor provides full control over which fields are copied and how nested mutable objects are handled.
What is the difference between shallow copy and deep copy in Java?
Shallow copy vs deep copy in java: shallow copy duplicates field values but copies reference-type fields by reference - original and copy share the same nested objects. Deep copy creates new independent instances of all mutable fields. The copy constructor in java program produces a deep copy when it explicitly creates new objects for each mutable reference field (arrays, nested classes).
How do you copy an object in Java?
How to copy an object in java: four main approaches - (1) Copy Constructor: define a constructor that takes the same class as parameter and manually copies fields; (2) java object cloning: implement Cloneable and override clone(); (3) Serialisation: serialise then deserialise - automatic deep copy for Serializable classes; (4) Copy Factory Method: static method returning a new copy. Copy constructor is the recommended approach.
Is a copy constructor better than clone() in Java?
Yes, for most cases. The copy constructor in java is generally preferred over clone() because: it requires no Cloneable interface, it throws no checked exceptions, it can access private fields directly, it provides explicit control over deep copy in java, and it avoids the confusing type-casting requirement of clone(). Joshua Bloch recommends copy constructors or copy factories over clone() in Effective Java.
What are the types of Java constructors?
The four main java constructor types: Default Constructor (no-arg, Java-provided if none defined), Parameterised Constructor (accepts arguments to set initial state), Copy Constructor (takes same-class object, creates independent duplicate - the focus of this article), and Private Constructor (prevents external instantiation - used in Singleton pattern). The java copy constructor example shows the copy constructor type in action.
Define the copy constructor in OOPS.
What is the use of a Copy Constructor?
Is it possible to create a deep copy using a copy constructor?
What is the main difference between a regular constructor and a copy constructor?
Updated on April 16, 2026
