Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

Object cloning in Java

Reference for copying objects in Java: Cloneable and clone(), shallow versus deep copies, copy constructors and factories, and why clone() is discouraged.

Copying an object means creating a second object with the same state. Assignment (b = a) copies only the reference, so both variables point at one object. Real copies come from Object.clone(), a copy constructor, a static copy factory, or a library such as serialization.

clone() is the original Java mechanism, but it is widely considered flawed: Cloneable is a marker interface with no methods, clone() is protected in Object, it creates objects without calling a constructor, and it throws a checked exception. Most modern code uses a copy constructor or copy factory instead. Arrays are the one place where clone() is idiomatic.

Java
int[] copy = original.clone();          // arrays: fine
Person p2 = new Person(p1);             // copy constructor

Try it

Java Shallow versus deep copy with clone()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Copy constructor, copy factory and arrays

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
protected Object clone() throws CloneNotSupportedException
Detail Description
Returns A new object of the same runtime class with all fields copied (shallow).
Throws CloneNotSupportedException if the class does not implement Cloneable.
Access protected in Object; override as public to expose it. Covariant return types let you declare public Team clone().

Usage notes

  • Shallow by default. super.clone() copies field values. Primitive fields are independent, but reference fields point at the same objects in the original and the copy. Mutable referenced objects must be cloned by hand.
  • No constructor runs for the copy, so invariants established in constructors and final-field initialization can be skipped. final fields holding mutable objects cannot be reassigned after super.clone().
  • Subclasses: every class in the hierarchy must cooperate by calling super.clone(), not new, or the copy will have the wrong type.
  • Arrays: array.clone() works without a cast to Cloneable and is a shallow copy; for arrays of arrays, copy each row.
  • Collections: use the copy constructor (new ArrayList<>(list)), List.copyOf (immutable snapshot), or Map.copyOf. These copy references to elements, not the elements.
  • Immutable classes never need copying; share them freely.
  • Records are copied by calling the constructor with the components, ideally via a "wither" method.
  • Serialization or JSON round-trips make deep copies but are slow and require serializable types.
  • Effective advice: provide a copy constructor or factory, and avoid implementing Cloneable in new classes unless a framework demands it.