Cloneable in Java

Last Updated : 15 Oct 2026

The Cloneable interface in Java indicates that a class supports cloning through the Object class's clone() method. It belongs to the java.lang package and is used to create a copy of an existing object.

In this chapter, you will learn about the Cloneable interface, the clone() method, how to clone objects in Java, and the limitations of object cloning.

What is Cloneable in Java?

Cloneable is a marker interface in Java. It does not declare any methods; instead, it indicates that an object's class permits cloning through the clone() method inherited from the Object class.

The Object class provides the clone() method, which creates a shallow copy of an object when cloning is supported. If a class does not implement Cloneable, calling super.clone() throws a CloneNotSupportedException.

To use the default cloning mechanism, a class typically implements Cloneable and overrides clone() with an appropriate access level.

Syntax of the clone() Method

The clone() method is declared in the Object class as follows:

protected native Object clone() throws CloneNotSupportedException;

The method creates and returns a copy of the object when cloning is supported. Because the method is declared protected, a class commonly overrides it to make cloning accessible to its callers.

Example of Cloneable in Java

The following example demonstrates how to implement the Cloneable interface and create a copy of a Student object using the clone() method.

Java

class Student implements Cloneable {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public Student clone() throws CloneNotSupportedException {
        return (Student) super.clone();
    }

    public static void main(String[] args) {
        Student s = new Student(101, "John");

        System.out.println(s.id + " " + s.name);

        try {
            Student s1 = s.clone();

            System.out.println(s1.id + " " + s1.name);
            System.out.println("Are both objects the same? " + (s == s1));
        } catch (CloneNotSupportedException e) {
            System.out.println("Unable to clone the Student object.");
        }
    }
}

Output:

101 John
101 John
Are both objects the same? false

In this example, the Student class implements Cloneable and overrides the clone() method. The call to super.clone() creates a shallow copy of the object. Both objects contain the same field values, but s and s1 refer to different objects.

How Cloning Works in Java

The following steps explain how object cloning works in Java.

  1. Implement Cloneable: The class implements the Cloneable interface to indicate that cloning is supported.
  2. Override clone(): The class overrides the clone() method to make it accessible as needed.
  3. Call super.clone(): The overridden method invokes super.clone() to create a shallow copy.
  4. Return the copy: The method returns the newly created object, which can be assigned to a reference variable of the appropriate type.

Types of Object Cloning in Java

Java object cloning is commonly discussed in terms of shallow copying and deep copying.

1. Shallow Copy

A shallow copy creates a new object and copies the values of its fields. Primitive fields receive their values, while reference fields continue to refer to the same objects as the original.

The default Object.clone() mechanism performs a shallow copy.

2. Deep Copy

A deep copy creates a new object and also creates independent copies of the mutable objects referenced by its fields, as required by the class's design. This usually requires additional implementation beyond calling super.clone().

Deep copying is useful when the original and copied objects must not share mutable nested objects.

Advantages of Cloneable in Java

The Cloneable interface and clone() method provide the following benefits:

  1. Object copying: They provide a standard mechanism for creating a copy of an existing object.
  2. Preserving field values: The default cloning mechanism copies the values of the object's fields.
  3. Customizable cloning: A class can override clone() to perform additional copying or validation.
  4. Shallow copying: The default mechanism can create a shallow copy without manually assigning every field.

Limitations of Cloneable in Java

Cloning has some limitations that developers should consider.

  1. Shallow copying by default: Referenced objects are not automatically copied, so mutable data may be shared.
  2. Exception handling: The clone() method can throw CloneNotSupportedException.
  3. Implementation requirements: The class must implement Cloneable for the default cloning mechanism to work.
  4. Complex deep copying: Classes with nested objects may require additional code to create independent copies.
  5. Limited flexibility: The cloning mechanism does not invoke a constructor to initialize the copied object's fields in the usual way.

Alternative Ways to Copy Objects in Java

Instead of using Cloneable, developers can use other approaches depending on their requirements.

  • Copy constructor: Create a constructor that accepts an existing object and copies its fields.
  • Factory method: Define a method that creates a new object based on an existing object's state.
  • Manual copying: Create a new object and explicitly copy the required fields.
  • Serialization-based copying: In suitable cases, serialize and deserialize an object graph to create a separate copy, provided the involved classes support the required serialization behavior.

A copy constructor is often easier to understand and maintain because the copying behavior is explicit.