Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Cloneable and Object.clone()

Reference for Cloneable and Object.clone(): how the marker interface works, shallow versus deep copies, array cloning and why copy constructors are preferred.

Cloneable is a marker interface with no methods. Implementing it tells Object.clone() that a class permits field-by-field copying. clone() is a protected native method on Object: it allocates a new object of the same runtime class, copies every field's value into it without calling any constructor, and returns it.

The copy is shallow. Primitive fields are copied by value, but reference fields are copied as references, so the original and the clone share the same nested objects. The design is widely considered flawed: it bypasses constructors, clashes with final fields, and forces casts. Arrays are the one place where clone() is routine.

Java
class Point implements Cloneable {
    int x, y;

    @Override
    public Point clone() {
        try {
            return (Point) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError(e);   // cannot happen: we are Cloneable
        }
    }
}

Try it

Java Shallow versus deep copy

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 Arrays, missing Cloneable and a copy constructor

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
Part Description
Cloneable Marker interface (no methods). Without it Object.clone() throws.
Object.clone() Protected; makes a shallow, constructor-free copy of the same runtime class.
Override Make it public, narrow the return type (covariant return), and drop the throws clause when you can.

Throws: CloneNotSupportedException (checked) if the object's class does not implement Cloneable.

Usage notes

  • Call super.clone() first in an override, so subclasses get the right runtime type. Never create the copy with new.
  • Shallow by default. Every mutable reference field you do not copy by hand is shared between original and clone. Immutable fields such as String are safe to share.
  • final fields cannot be re-assigned after super.clone(), which makes deep cloning of final mutable fields awkward. This is one reason to avoid clone() in new designs.
  • Constructors and initializers do not run on the clone, so invariants that depend on them can be violated.
  • Arrays implement Cloneable with a public clone() that returns the correct array type. For a multidimensional array only the outer array is copied.
  • Prefer alternatives: a copy constructor, a static factory such as List.copyOf(), or immutable types that need no copying.
  • Collections such as ArrayList and HashMap have a public clone() that is also shallow: the elements are shared.
  • Enums and records do not need cloning. Enum clone() is final and always throws.