Ad Space — Top Banner

CloneNotSupportedException

Java Programming Language

Severity: Minor

What Does This Error Mean?

CloneNotSupportedException is thrown when you try to clone an object that has not been set up to support cloning. In Java, to clone an object, the class must implement the Cloneable interface. If it does not, calling .clone() throws CloneNotSupportedException. The fix is to either implement Cloneable in the class, or use a different way to copy the object.

Affected Models

  • Java 1.0 and later
  • Java SE
  • Java EE
  • Android

Common Causes

  • Calling .clone() on an object whose class does not implement the Cloneable interface
  • A superclass implements Cloneable but a subclass overrides clone() and throws CloneNotSupportedException
  • Trying to clone a class from a third-party library that does not support cloning
  • Forgetting to implement Cloneable when overriding the clone() method
  • Calling super.clone() inside a clone() method when the parent class does not support cloning

How to Fix It

  1. To enable cloning for your class, implement the Cloneable interface: public class MyClass implements Cloneable

    Cloneable is a marker interface — it has no methods to implement. It just signals to Java that cloning is allowed for this class.

  2. Override the clone() method in your class and call super.clone(). Make the method public (the default in Object is protected).

    Example: @Override public MyClass clone() throws CloneNotSupportedException { return (MyClass) super.clone(); }

  3. Consider using a copy constructor instead of clone() — it is simpler, clearer, and works better with deep copies.

    Example: public MyClass(MyClass original) { this.name = original.name; this.value = original.value; } — then copy with: MyClass copy = new MyClass(original);

  4. For deep cloning (where the object contains other objects that also need to be copied), clone() is tricky. A copy constructor or serialization-based copy is often easier.

    Java's built-in clone() creates a shallow copy — it copies reference values, not the objects they point to. Deep copy requires manually cloning each nested object.

  5. If you are using a library class that does not support cloning, check if the library provides a copy method, builder pattern, or factory method to create a copy.

    Most modern libraries prefer copy constructors or builders over clone(). Check the documentation or look for methods like copy(), duplicate(), or of().

When to Call a Professional

CloneNotSupportedException is straightforward to fix. Either implement Cloneable properly, or use a copy constructor or factory method instead — which is the modern preferred approach.

Frequently Asked Questions

What does the Cloneable interface actually do?

Cloneable is a 'marker interface' — it has no methods. Its only purpose is to act as a flag that tells Java: this class is OK to clone. Without implementing Cloneable, calling super.clone() (which is the method inside Object that does the actual copying) will throw CloneNotSupportedException. Java checks for Cloneable before allowing clone() to proceed.

Should I use clone() or write a copy constructor?

Most experienced Java developers prefer copy constructors over clone(). Clone() has several quirks — it returns Object (requiring a cast), it creates shallow copies, and the Cloneable mechanism is unusual. A copy constructor is simpler: public MyClass(MyClass other) — it is clear, type-safe, and easier to make deep. Effective Java by Joshua Bloch (the classic Java book) recommends avoiding clone() for most use cases.

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

A shallow copy copies the reference values — both the original and the copy point to the same nested objects. Changing a nested object in the copy also changes it in the original. A deep copy creates entirely new objects for each nested object — the copy is completely independent. Java's built-in clone() creates a shallow copy. For deep copies, you need to manually clone each nested object.