Java core
What makes a class immutable and why use immutability?
What makes a class immutable?
An immutable class is one whose instances cannot be changed after creation. Once the
object is created, its state (field values) cannot be modified.
Key rules for making a class immutable:
1. Mark the class as final → Prevents subclassing, which could add mutability.
2. Make all fields private and final → private prevents direct access, final
ensures they can’t be reassigned.
3. No setters → Do not provide methods that change field values after object creation.
4. Initialize all fields in the constructor → Ensures the object’s state is fully set at
creation time.
5. Defensive copies for mutable fields → If your class has fields like Date or List ,
don’t expose them directly. Example: return a copy in the getter, and copy inputs in
the constructor.
6. Don’t allow methods to modify state → Every method should return a new object
instead of altering the current one.
Example of an immutable class:
public final class Person {
private final String name;
private final int age;
public Person(String name, int age) {
[Link] = name;
[Link] = age;
}
// No setters
public String getName() { return name; }
public int getAge() { return age; }
// If change needed, return new object
public Person withAge(int newAge) {
return new Person([Link], newAge);
}
}
Real-time usages of immutability:
1. Java String class
Immutable for security (e.g., passwords, class loading) and caching.
If mutable, someone could alter the value after authentication — huge security risk.
String password = "admin123";
authenticate(password);
[Link](0, 'x'); // Imagine this was possible
2. Concurrent Applications
Multiple threads can read the balance without risk of corruption.
Example: Money or Balance objects in financial systems are immutable, avoiding race
conditions.
Money balance = new Money(100);
Money updated = [Link](50); // returns new object
3. Caching & Collections
Immutables work well as keys in hash-based collections (HashMap ,
HashSet ).Example: String and wrapper classes (Integer , Long , etc.) are
[Link] keys were mutable, hash codes could change → maps would break.
4. Domain-driven Design (DDD)
Value Objects like Address , Money , Coordinates are modeled as immutable for
consistency.
Address addr = new Address("221B Baker Street", "London");
// If you need to change city
Address newAddr = new Address([Link](), "Manchester");
Keeps business rules predictable and prevents side-effects.
5. Functional Programming (Java 8+)
Streams and lambdas process immutable data safely without side effects.
List<Integer> nums = [Link](1, 2, 3);
List<Integer> squared = [Link]()
.map(n -> n * n)
.toList(); // immutable result
Prevents accidental modification while processing in pipelines.
6. Configuration & Constants
Objects like application configuration, environment variables, API keys must
never change once loaded.
Example: Spring Boot @ConfigurationProperties often uses immutable classes for
reliability.
In short: Immutability gives safety, simplicity, and reliability at the cost of flexibility.
That’s why core Java classes like String, Integer, LocalDate are immutable.