Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What problems does Object.clone() have, and what are better ways to implement the prototype idea of copying an existing object in Java?

Object.clone() has an awkward contract: Cloneable is a marker interface with no clone() method, Object.clone() is protected and throws a checked exception, and it makes a shallow copy without calling any constructor. That shares mutable fields between the copies, bypasses the invariants a constructor would check, and does not work with final fields that need a deep copy. Copy constructors or static copy factories, such as new ArrayList<>(other), are the better way, and immutable objects often remove the need to copy at all.

Why this question is asked

The prototype pattern means creating a new object by copying an existing one instead of building it from scratch. Java seemed to support it directly with clone(), and older code bases are full of it. Interviewers ask about clone() because it is a small API with many design mistakes in it, and the answer shows whether we understand shallow and deep copies, invariants and why constructors matter. In this answer, we will recap how clone() is meant to be used, go through its problems one by one with code, and then look at the alternatives that modern Java code prefers.

1A Quick Recap of How clone() Is Meant to Work

To make a class cloneable, we implement the Cloneable interface and override clone(), calling super.clone(). Object.clone() then allocates a new object of the same class and copies every field value into it, bit for bit. If the class does not implement Cloneable, Object.clone() throws CloneNotSupportedException.

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 implement Cloneable
}
}
}
Point a = new Point(); a.x = 1;
Point b = a.clone();
b.x = 2;
System.out.println(a.x); // 1, the copy is a separate object

For a class with only primitive fields this works. The problems start as soon as the class is more interesting.

2Problem 1: An Awkward Contract

Cloneable is a marker interface: it has no methods at all. Implementing it does not give a class a public clone() method; it only changes how the protected Object.clone() behaves. So we cannot write a method that accepts any Cloneable and clones it, because Cloneable has nothing to call.

Object.clone() is protected, returns Object and declares the checked CloneNotSupportedException. Every class must override it to make it public, cast the result, and catch an exception that cannot happen. None of this is enforced by the compiler, so a class can implement Cloneable and forget to override clone(), and callers outside the package simply cannot use it.

3Problem 2: Shallow Copies Share Mutable State

Object.clone() copies field values. For a reference field, the value is the reference, so the original and the copy point to the same object. This is a shallow copy.

Java
class Order implements Cloneable {
List<String> items = new ArrayList<>();
@Override public Order clone() {
try { return (Order) super.clone(); } catch (CloneNotSupportedException e) { throw new AssertionError(e); }
}
}
Order original = new Order();
original.items.add("book");
Order copy = original.clone();
copy.items.add("pen");
System.out.println(original.items); // [book, pen]: the list is shared
What a shallow copy shares
FieldOriginalCopy after super.clone()
int x11, an independent value
String name"A"same String, harmless because it is immutable
List<String> itemslist @7a1same list @7a1, changes show in both
final List<String> tagslist @3c2same list, and clone() cannot replace it

Notice that the change to the copy appears in the original. To fix it, clone() must copy each mutable field itself, for example copy.items = new ArrayList<>(items). Every new mutable field added later must be remembered in clone() too, and nothing warns us if it is forgotten.

4Problem 3: No Constructor, No Invariants, No Final Fields

Object.clone() creates the new object without running any constructor. If the constructor checked an invariant, registered the object somewhere, or assigned a unique id, the copy skips all of it. A copy of an entity with an id field gets the same id, which is rarely what we want.

Final fields make it worse. A deep copy needs to assign a new list to the copy's field after super.clone() returns, but a final field can only be assigned in a constructor. So a class with a final mutable field cannot be deep-copied with clone() without giving up final, or using reflection. In practice, clone() pushes us toward mutable, non-final fields, which is the opposite of good design.

5Better: Copy Constructors and Static Copy Factories

A copy constructor is a constructor that takes another instance of the same class. A static copy factory is a static method that does the same, such as Order.copyOf(other). Both are ordinary Java code: they run the constructor, they can assign final fields, and they decide explicitly which fields are deep-copied.

Java
final class Order {
private final List<String> items;
Order(List<String> items) { this.items = new ArrayList<>(items); } // invariant checks can go here
Order(Order other) { this(other.items); } // copy constructor
static Order copyOf(Order other) { return new Order(other); } // static copy factory
List<String> items() { return Collections.unmodifiableList(items); }
}
Order a = new Order(List.of("book"));
Order b = new Order(a);
System.out.println(a.items() == b.items()); // false, separate lists

The JDK uses this style itself. new ArrayList<>(other) is a copy constructor, and List.copyOf(other) and Map.copyOf(other) are static copy factories that return immutable copies. A copy constructor can also accept an interface type, so new ArrayList<>(someSet) converts as well as copies, which clone() can never do. The one place clone() is still fine is arrays: array clone() is public, needs no cast since Java 5, and gives a shallow copy of the array.

6Often We Do Not Need a Copy at All

We copy an object so that one side cannot see the other side's changes. If the object is immutable, nobody can change it, so both sides can safely share the same instance. Records with immutable components, String, LocalDate and List.of(...) results are all safe to share. When we need a variation, we create a new object with the changed value, for example with a withPrice() method, and the original stays untouched. This is the real lesson of the prototype pattern in modern Java: share immutable prototypes, and create modified copies explicitly when needed.

7Follow-up questions

Is clone() deprecated?

No, Object.clone() is not deprecated, and Cloneable still exists. But the general guidance in the Java community for many years has been to avoid implementing Cloneable in new classes and to use copy constructors or copy factories instead, except for arrays.

What is the difference between a shallow and a deep copy?

A shallow copy copies the fields of the object, so reference fields in the copy point to the same objects as the original. A deep copy also copies the objects those fields refer to, recursively as far as needed, so the copy shares no mutable state with the original. Immutable parts, such as Strings, can be shared even in a deep copy.

How does a subclass interact with a copy constructor?

A copy constructor creates exactly the class it belongs to, so new Animal(dog) creates an Animal, not a Dog. If we need a copy that keeps the runtime type, we add a copy() method that each subclass overrides, implemented with its own copy constructor.

8Conclusion

In this answer, we saw why Object.clone() is a poor tool for the prototype idea. Cloneable has no methods and Object.clone() is protected, so the contract is awkward; it makes shallow copies that share mutable fields; and it creates objects without a constructor, skipping invariants and clashing with final fields. Copy constructors and static copy factories, like new ArrayList<>(other) and List.copyOf(), are explicit and safe, and immutable objects often make copying unnecessary.

What to say in the interviewAbout 70 seconds aloud

Open with

clone has several problems. The contract is awkward: Cloneable is just a marker interface with no clone method, and Object.clone is protected, returns Object and throws a checked exception, so every class has to override it, cast and catch something that never happens.

Then, if they want more

By default it makes a shallow copy, so mutable fields like lists are shared between the original and the copy, and a change in one shows up in the other unless I remember to copy each field.

It also creates the object without calling a constructor, so any invariant checks or id assignment in the constructor are skipped, and since final fields can only be assigned in a constructor, I cannot deep-copy a final list in clone without dropping final.

So in new code I use a copy constructor or a static copy factory, the way the JDK does with new ArrayList of another collection or List.copyOf. That runs the constructor, works with final fields and makes the deep-copy decisions explicit. And often the best answer is immutability, because an immutable object can simply be shared instead of copied.

About this answer

Last updated
12 November 2026
Versions it names
Java 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What problems does Object.clone() have, and what are better ways to implement the...": https://scrivio-interview-library.netlify.app/java/creational-patterns/950f02353b/