CKTIS / Java
Copy constructors, shallow copies, and deep copies
A copy constructor creates a new instance from an existing one. A shallow copy still shares nested objects, while a deep copy separates the mutable state covered by the copy contract.
Version: Java SE 21
Syntax
T(T source) { /* initialize the new instance from source */ }
T copy = new T(source);Copy constructor, shallow and deep copying: the constructor is how you create a new instance; shallow or deep describes what it shares with the source. A copy constructor is an ordinary constructor that takes an instance of the same class, such as T(T source). Java SE 21 does not generate it automatically. In new T(source), overload selection happens at compile time, and the constructor implementation decides what to copy or share. By contrast, T alias = source copies only the reference and creates no object.
A shallow copy creates a new outer object but copies field values as-is: primitive values and references alike. A mutation to a shared mutable object is visible through both instances. A final reference field prevents reassignment, not mutation of the referenced object. The base implementation of Object.clone() makes a shallow copy and throws CloneNotSupportedException if the object does not implement Cloneable. A copy constructor does not require that interface.
A deep copy also recreates nested mutable objects within the scope of the copy contract, so mutations to that state do not cross between the original and the copy. Immutable objects can be shared. A new container alone is not enough if its mutable elements are still shared.
To preserve cycles and shared nodes in an object graph, record each original-to-copy pair by identity, for example in an IdentityHashMap, before following that node's references. Repeated references then lead to the same copied node, and cycles do not cause infinite recursion.
Examples
public class Main {
static final class Box {
private final int[] values;
private Box(int[] values) {
this.values = values;
}
Box(Box source) {
this(source.values.clone());
}
static Box shallowCopyOf(Box source) {
return new Box(source.values);
}
}
public static void main(String[] args) {
Box original = new Box(new int[] {1});
Box shallow = Box.shallowCopyOf(original);
Box deep = new Box(original);
System.out.println("Distinct boxes: " + (original != shallow) + ", " + (original != deep));
System.out.println("Shared arrays: " + (original.values == shallow.values)
+ ", " + (original.values == deep.values));
shallow.values[0] = 7;
deep.values[0] = 9;
System.out.println("Values: original=" + original.values[0]
+ ", shallow=" + shallow.values[0] + ", deep=" + deep.values[0]);
}
}Result
Distinct boxes: true, true Shared arrays: true, false Values: original=7, shallow=7, deep=9
Both copies are new `Box` objects; only `shallow` shares its array with `original`. Mutating through `shallow` sets the value seen in `original` to `7`, while mutating through `deep` sets only its own value to `9`. Cloning an `int[]` is enough because its elements are primitives; cloning a reference array does not copy the referenced objects.
Related lessons
- Java · Lesson 6 · Advanced · Version: 2