Skip to main content
PreparationZone Logo
PrepZone

Shallow and Deep Copies

What clone() really does, why it surprises people, and safer ways to copy an object.

Pass checkpoint below

Why this matters

  • The aliasing bug this causes is among the hardest to trace: you copy an object, change the copy, and the original changes too.
  • Cloneable is one of the JDK's acknowledged design mistakes, and knowing why is more useful than knowing how to use it.
  • Defensive copying is the practical skill underneath all of it, and it protects encapsulation at every API boundary.
Caller
cartholds reference #A1
Method parameter
inputcopy of reference #A1
Cart object #A1items = [book]

input.add(pen) is visible to the caller. input = new Cart() is not — it only repoints the copy.

Java copies the value in the variable. For an object that value is the reference, so the method can change the object's fields but cannot repoint the caller's variable.

Shallow versus deep

Java
Loading compiler…
Java
Loading compiler…
AspectShallow copyDeep copy
New top-level objectYesYes
Nested mutable objectsShared with the originalAlso duplicated
IndependenceOnly for primitive and immutable fieldsComplete
CostCheapProportional to the object graph
Safe whenEvery field is primitive or immutableAlways
  • New top-level object

    Shallow copyYes
    Deep copyYes
  • Nested mutable objects

    Shallow copyShared with the original
    Deep copyAlso duplicated
  • Independence

    Shallow copyOnly for primitive and immutable fields
    Deep copyComplete
  • Cost

    Shallow copyCheap
    Deep copyProportional to the object graph
  • Safe when

    Shallow copyEvery field is primitive or immutable
    Deep copyAlways

A shallow copy is perfectly correct when nothing it points at can change.

How clone() actually works

Object.clone() is protected and native. It allocates a new object of the same class and copies every field bit for bit — references included. To expose it you must do two things that look arbitrary.

Java
Loading compiler…

Why this is considered broken

  • Cloneable declares no methods. It is a marker that changes the behaviour of a protected method on a different class — a mechanism found nowhere else in the language.
  • clone() bypasses constructors. No validation runs, and final fields cannot be assigned, so invariants you rely on may not hold.
  • It throws a checked exception you cannot encounter. Every implementation needs the unreachable catch block above.
  • It is shallow by default, in a method whose name implies otherwise.
  • Subclasses inherit the problem. Any subclass adding a mutable field silently gets a broken clone unless it overrides again.

For a deep clone you must fix up each mutable field yourself:

Java
Loading compiler…

Every mutable field must be remembered. Adding a field later and forgetting this method is the standard way this breaks months after it was written.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 3 sections

  • Better ways to copy
  • Defensive copying at the boundary
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.