DEV Community

yash agarwal
yash agarwal

Posted on

đź§  What Is Immutability?

An immutable object is one whose state cannot change after creation.
Once you create it, that’s it — no more modifications.

For example, Java’s String class is immutable:

String name = "Yash";
name.concat(" Agarwal");
System.out.println(name); // Still prints "Yash"

concat() creates a new String instead of modifying the original.

âš™ Why Immutability Matters

Here’s why immutable objects are a developer’s best friend:

✅ Thread-Safety – Immutable objects can be shared across threads safely without synchronization.

đź§© Predictable Behavior - Their state never changes, so debugging is easier.

🧠 Simpler Design – You don’t have to worry about side effects.

💡 Cache Friendly – Immutable objects can be cached or reused without fear of modification.

đź§± How to Make a Class Immutable

To make your own class immutable, follow these 5 golden rules:

  1. Mark the class as final

So no one can subclass and modify its behavior.

public final class Employee {

  1. Make all fields private and final

This ensures they can’t be reassigned after construction.

private final String name;
private final int id;

  1. Initialize fields through the constructor only

All values should be set once, during object creation.

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

  1. No setters!

Avoid any method that can modify the internal state.

❌ Bad:

public void setName(String name) { this.name = name; }

âś… Good:

public String getName() { return name; }

  1. Return deep copies of mutable objects

If your class holds references to mutable objects, protect them.

private final Date joiningDate;

public Date getJoiningDate() {
return new Date(joiningDate.getTime()); // Defensive copy
}

This ensures outside code can’t modify your object indirectly.

🚀 Example: Immutable Class in Action
public final class Employee {
private final String name;
private final int id;
private final Date joiningDate;

public Employee(String name, int id, Date joiningDate) {
    this.name = name;
    this.id = id;
    this.joiningDate = new Date(joiningDate.getTime());
}

public String getName() { return name; }
public int getId() { return id; }

public Date getJoiningDate() {
    return new Date(joiningDate.getTime());
}
Enter fullscreen mode Exit fullscreen mode

}

âś… Thread-safe
âś… No side effects
âś… Clean and predictable

đź§© Bonus: Records (Java 14+)

From Java 14 onward, records make creating immutable data classes effortless.

public record Employee(String name, int id, LocalDate joiningDate) {}

Records automatically:

Make fields final

Generate constructors & accessors

Prevent setters

This is the modern Java way to achieve immutability with minimal boilerplate.

đź§­ Conclusion

Immutability isn’t just a design principle — it’s a mindset.
When you stop mutating state and start creating predictable, unchangeable objects, your code becomes:

Easier to reason about

Safer in concurrent environments

More reliable and bug-resistant

“Immutable code is clean code. And clean code scales.”

Top comments (0)