Clean • Professional
In Java, object cloning and immutable class design are advanced concepts related to object copying and data protection.
Both are crucial for building secure, reliable, and thread-safe applications.
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

| 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.
CloneNotSupportedException occurs.clone() only performs shallow copy by default.An immutable class is a class whose objects cannot be modified after creation.
All field values remain constant, ensuring data safety and thread safety.
final (cannot be extended).private and final.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.
| 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 |
clone() → Creates object copy.Cloneable → Avoid exceptions.String, Integer, LocalDate, etc.