Cosmic Module
J
Qubits of DPK
March 15, 2026
Core Java
Layman Explanation
Imagine a birth certificate. Once issued, nobody can change your name, date of birth, or parents on it. It's fixed forever. That's an immutable object — created once, never changed.
Real World Analogy
A printed book. Once printed, the words never change. Everyone reading the same book gets identical content. No one can silently modify chapter 3 while you're reading chapter 5. Safe, reliable, consistent.
What is an Immutable Class?
An immutable class is one whose objects' state cannot be changed after creation.
Java's most famous immutable class: String
java
QUBITS OF DPK
Other built-in immutable classes:
javascript
QUBITS OF DPK
Rules to Create an Immutable Class
There are 5 mandatory rules:
Rule 1 — Declare class as final
java
QUBITS OF DPK
Prevents a subclass from overriding methods and breaking immutability.
Rule 2 — All fields must be private final
java
QUBITS OF DPK
Rule 3 — No setters
java
QUBITS OF DPK
Rule 4 — Initialize all fields in constructor
java
QUBITS OF DPK
Rule 5 — Defensive copy for mutable fields
java
QUBITS OF DPK
Complete Immutable Class Example
java
QUBITS OF DPK
Why Immutable Classes Are Valuable
1. Thread Safety (FREE)
java
QUBITS OF DPK
2. Safe as Map Keys and Set Elements
java
QUBITS OF DPK
3. Easy to Cache (Memoize)
java
QUBITS OF DPK
4. Failure Atomicity
java
QUBITS OF DPK
Java Records — Immutable by Design (Java 16+)
java
QUBITS OF DPK
Production Use Cases
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
🆚 Mutable vs Immutable
30-Second Interview Answer
"An immutable class is one whose state cannot change after creation. The 5 rules: declare class final (prevent subclassing), all fields private final (no reassignment), no setters, initialize all fields in constructor, and defensive copy for mutable fields (Date, List, arrays) in both constructor and getter. Benefits: thread safety without synchronization, safe as HashMap keys (stable hashCode), easy caching, and failure atomicity. String, Integer, and all wrapper classes are immutable. Java Records (Java 16+) provide immutability automatically with less boilerplate."
Interview Questions & MAANG-Level Answers
Q1. What are the rules to create an immutable class?
Five rules: (1) final class — prevents subclass from breaking immutability by adding setters. (2) private final fields — private prevents direct access, final prevents reassignment. (3) No setters — no modification after construction. (4) Initialize all fields in constructor — only time values are set. (5) Defensive copy for mutable fields — Date, arrays, List must be copied in constructor (so original can't affect object) and in getters (so caller can't modify internals). Miss any one rule and immutability is compromised.
Q2. Why is String immutable in Java?
Three reasons: (1) String Pool — multiple references share same object safely only if it can't be mutated. If a could change "hello", all references to that pool object would see the change. (2) Thread safety — String is used everywhere (DB URLs, passwords, class names) and needs to be safely shared across threads without synchronization. (3) Security — ClassLoader uses String names; if a String could be mutated after permission check but before use, a malicious class could be loaded. Also enables hashCode caching since value never changes.
Q3. What is the difference between final and immutable?
final on a variable means the variable cannot be reassigned — but if it holds a mutable object, the object itself can still be modified:
java
QUBITS OF DPK
Immutability means the object's state cannot change, regardless of how many references point to it. final is about the reference; immutable is about the object.
Q4. How does immutability help with thread safety?
Immutable objects have no state that can change, so concurrent reads by multiple threads are completely safe — no synchronization needed. If Thread A and Thread B both read Money.getAmount() simultaneously, they always get the same value. No race conditions possible. This is why the JVM internally uses immutable String for class names, package names, and constant pool entries. In concurrent systems design, "shared nothing" and "shared immutable" are the two safest patterns.
Q5. What is a defensive copy and when is it needed?
A defensive copy is creating a copy of a mutable object instead of sharing the reference, to prevent external code from modifying internals:
java
QUBITS OF DPK
Needed for: Date, arrays, ArrayList, HashMap — any mutable object stored in an immutable class. Java 8+ LocalDate/LocalDateTime are immutable themselves — no defensive copy needed.