Object Cloning & Immutable Class Design in Java
In Java, object cloning and immutable class design are advanced concepts related to object copying and data protection.
- Cloning allows you to create an exact copy of an object.
- Immutability ensures that object data cannot be changed once created.
Both are crucial for building secure, reliable, and thread-safe applications.
1. Object Cloning in Java
Object cloning means creating a copy of an existing object with the same values for its fields.
In Java, cloning is achieved using the clone() method of the Object class and the Cloneable interface.
Syntax
protected Object clone() throws CloneNotSupportedException
This method returns a shallow copy of the object.
Example – Object Cloning
class Student implements Cloneable {
int id;
String name;
Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone(); // Shallow copy
}
}
public class CloningExample {
public static void main(String[] args) throws CloneNotSupportedException {
Student s1 = new Student(101, "Amit");
Student s2 = (Student) s1.clone();
System.out.println("Original: " + s1.name);
System.out.println("Cloned: " + s2.name);
}
}
Output:
Original: Amit
Cloned: Amit
2. Types of Cloning

| Type | Description | Copy Depth |
|---|---|---|
| Shallow Copy | Creates a copy of the object, but does not copy nested objects. Both objects share the same reference for inner objects. | One-level |
| Deep Copy | Copies all fields and creates new objects for nested references. | Multi-level |
Example – Deep Cloning
class Address implements Cloneable {
String city;
Address(String city) { this.city = city; }
public Object clone() throws CloneNotSupportedException { return super.clone(); }
}
class Employee implements Cloneable {
int id;
Address address;
Employee(int id, Address address) { this.id = id; this.address = address; }
@Override
public Object clone() throws CloneNotSupportedException {
Employee e = (Employee) super.clone();
e.address = (Address) address.clone(); // Deep copy
return e;
}
}
public class DeepCloneExample {
public static void main(String[] args) throws CloneNotSupportedException {
Address addr = new Address("Delhi");
Employee e1 = new Employee(1, addr);
Employee e2 = (Employee) e1.clone();
e2.address.city = "Mumbai"; // only cloned object changes
System.out.println("Original City: " + e1.address.city);
System.out.println("Cloned City: " + e2.address.city);
}
}
Output:
Original City: Delhi
Cloned City: Mumbai
The original object remains unaffected — Deep Clone successful.
3. Common Issues in Cloning
- Class must implement Cloneable, otherwise
CloneNotSupportedExceptionoccurs. clone()only performs shallow copy by default.- Complex nested objects require manual deep copy logic.
- Recommended alternative: Copy constructors or serialization-based cloning.
4. Immutable Class Design
An immutable class is a class whose objects cannot be modified after creation.
All field values remain constant, ensuring data safety and thread safety.
Rules to Create an Immutable Class
- Declare the class as
final(cannot be extended). - Make all fields
privateandfinal. - Do not provide setter methods.
- Initialize all fields using a constructor.
- For mutable objects, perform deep copies in constructors and getters.
Example – Immutable Class
final class Employee {
private final String name;
private final int id;
public Employee(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() { return name; }
public int getId() { return id; }
}
public class ImmutableExample {
public static void main(String[] args) {
Employee e = new Employee("John", 101);
System.out.println(e.getName() + " - " + e.getId());
}
}Output:
John - 101
No setter methods = object cannot be changed.
Example – Handling Mutable Fields
final class Student {
private final String name;
private final java.util.Date dob;
public Student(String name, java.util.Date dob) {
this.name = name;
this.dob = new java.util.Date(dob.getTime()); // deep copy
}
public String getName() { return name; }
public java.util.Date getDob() {
return new java.util.Date(dob.getTime()); // return copy
}
}
This ensures true immutability, even for mutable fields like Date.
Difference Between Cloning and Immutable Design
| Feature | Cloning | Immutable Class |
|---|---|---|
| Purpose | To copy object | To prevent modification |
| Data Change | Allowed | Not allowed |
| Performance | Faster (shallow) or slower (deep) | Stable and thread-safe |
| Implementation | clone() method | final, private, no setters |
| Example | Student s2 = (Student)s1.clone(); | Immutable Employee class |
Points to Remember
clone()→ Creates object copy.- Implement
Cloneable→ Avoid exceptions. - Prefer deep copy for independent objects.
- Immutable classes ensure data integrity and thread safety.
- Common immutable classes in Java:
String,Integer,LocalDate, etc.
