C

Core Java tutorial for beginners

Clean • Professional

Java - Object Cloning & Immutable Class Design with Examples

5 minute

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

learn code with durgesh images

TypeDescriptionCopy Depth
Shallow CopyCreates a copy of the object, but does not copy nested objects. Both objects share the same reference for inner objects.One-level
Deep CopyCopies 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 CloneNotSupportedException occurs.
  • 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

  1. Declare the class as final (cannot be extended).
  2. Make all fields private and final.
  3. Do not provide setter methods.
  4. Initialize all fields using a constructor.
  5. 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

FeatureCloningImmutable Class
PurposeTo copy objectTo prevent modification
Data ChangeAllowedNot allowed
PerformanceFaster (shallow) or slower (deep)Stable and thread-safe
Implementationclone() methodfinal, private, no setters
ExampleStudent 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.

Article 0 of 0