Last Updated : 16 May, 2025
In Java, cloning is a process in which we create an exact copy (clone) of an object. For example, an object containing a list has values {3,5,7}, so the cloned object also contains the same values as the original object {3, 5, 7}. The clone() method in the Object class is used for performing the cloning of an object.
Note: If we are cloning an object, then the class must have implemented the Cloneable interface otherwise, it throws CloneNotSupportedException.
Types of Cloning in JavaThere are two types of cloning or copying in Java mentioned below:
1. Shallow CloningShallow cloning in Java creates a new object, but the new object that is created has the same reference as the original object, so instead of creating an actual copy, it involves copying the references.
Important Characteristics:
Example: Performing a shallow copy using the Clone() method.
Java
// Java program to show shallow cloning
class Address implements Cloneable
{
String city;
public Address(String city) {
this.city = city;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Geek implements Cloneable
{
String name;
int age;
Address address;
public Geek(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone(); // Shallow cloning
}
}
public class Main
{
public static void main(String[] args)
{
try {
Address address = new Address("Bhopal");
Geek p1 = new Geek("Geek", 35, address);
// Shallow clone
Geek p2 = (Geek) p1.clone();
System.out.println("Original Person: " + p1.name + ", " + p1.age + ", " + p1.address.city);
System.out.println("Cloned Person: " + p2.name + ", " + p2.age + ", " + p2.address.city);
// Modifying the original object's address and age
p1.address.city = "Noida";
p1.age = 30;
System.out.println("\nAfter modifying the original address:");
System.out.println("Original Person: " + p1.name + ", " + p1.age + ", " + p1.address.city);
System.out.println("Cloned Person: " + p2.name + ", " + p2.age + ", " + p2.address.city);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Original Person: Geek, 35, Bhopal Cloned Person: Geek, 35, Bhopal After modifying the original address: Original Person: Geek, 30, Noida Cloned Person: Geek, 35, Noida
Explanation: Here, we are performing shallow copying with the help of Clone() method and both the original and cloned object shared the same Address reference. When the original object address is changed so the changes are reflected on the cloned object as well. Primitive fields like age are copied by value, so changes in the original's age do not affect the clone.
Deep cloning refers to creating a complete, independent copy of an object, including copies of any referenced objects. Deep cloning is different from shallow cloning because it ensure that the original object do not share the references not even for the mutable fields.
Important Characteristics:
Example 2: Performing a Deep copy using the Clone() method.
Java
// Deep cloning in Java using clone() method
class Address implements Cloneable
{
String city;
public Address(String city) {
this.city = city;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Geek implements Cloneable
{
String name;
int age;
Address address;
public Geek(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
@Override
public Object clone() throws CloneNotSupportedException {
Geek cloned = (Geek) super.clone();
// Deep clone the address object
cloned.address = (Address) address.clone();
return cloned;
}
}
public class Main
{
public static void main(String[] args)
{
try {
Address address = new Address("Bhopal");
Geek p1 = new Geek("Geek", 35, address);
// Deep clone
Geek p2 = (Geek) p1.clone();
System.out.println("Original Person: " + p1.name + ", " + p1.age + ", " + p1.address.city);
System.out.println("Cloned Person: " + p2.name + ", " + p2.age + ", " + p2.address.city);
// Modifying the original object's address and age
p1.address.city = "Noida";
p1.age = 30;
System.out.println("\nAfter modifying the original address:");
System.out.println("Original Person: " + p1.name + ", " + p1.age + ", " + p1.address.city);
System.out.println("Cloned Person: " + p2.name + ", " + p2.age + ", " + p2.address.city);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Original Person: Geek, 35, Bhopal Cloned Person: Geek, 35, Bhopal After modifying the original address: Original Person: Geek, 30, Noida Cloned Person: Geek, 35, Bhopal
Explanation: Here, we are performing deep cloning with the help of Clone() method. The Geek class overrides the clone() method to also clone the Address object it contains and because of this the original and cloned objects have independent Address objects. So, changes to the original address do not affect the clone address.
Cloneable InterfaceThe cloneable interface is a an important interface to understand when we want to perform the cloning operation. The Cloneable is a marker interface ( without containing any fields or methods ) that indicates to the Java Virtual Machine (JVM) that an object of a class can be cloned. The cloneable interface is necessary to allow objects to be cloned using the clone() method of the object class.
Syntax:
class ClassName implements Cloneable {
// fields and methods
@override
public Object clone() throws CloneNotSupportedException {return super.clone();
}
}
Important Characteristics:
Example 3: Try to use Clone method without implementing the Cloneable Interface.
Java
// Java program to show CloneNotSupportedException
class Address
{
String city;
public Address(String city) {
this.city = city;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
// Do not implements Cloneable here
class Geek
{
String name;
int age;
Address address;
public Geek(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
}
public class Main
{
public static void main(String[] args)
{
try {
Address address = new Address("Bhopal");
Geek p1 = new Geek("Geek", 35, address);
// This will throw CloneNotSupportedException because Geek does NOT implement Cloneable
Geek p2 = (Geek) p1.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
}
Output:
Explanation: Here, the Geek class does not implement Cloneable and when we call Clone() method it throws a CloneNotSupportedException. To avoid this exception, the class must implement Cloneable
Shallow Cloning vs Deep CloningThe table below demonstrates the difference between shallow cloning and deep cloning.
Aspect
Shallow Cloning
Deep Cloning
Mutable objects
It shares the same references
It creates independent copies of referenced objects
Object Reference
It only copies the reference, not the object.
It recursively copies all referenced objects
Complexity
It is simpler and faster
It is more complex and slower because of recursive copying.
Use Case
Suitable when shared objects are acceptable and does not require modification of objects.
Used when full independence between original and clone is required
Default Behaviour
Java's clone() performs shallow cloning by default
Requires manual handling (overriding clone()) for deep cloning
Important Points:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4