Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Hard · Free

How do you design an immutable class? Why immutability helps concurrency?

FresherFundamentalsOOP & Language Fundamentals
Model answer

Immutable classes

Immutable objects never change after construction—safe to share across threads without locks.

java
final class Money {
  private final BigDecimal amount;
  Money(BigDecimal a) { this.amount = a; }
  Money add(Money other) { return new Money(amount.add(other.amount)); }
}
  • Make class final, fields final, no setters.
  • Defensive copy mutable inputs (Date, collections).
  • Record classes (Java 16+) for data carriers.
Link to concurrency: immutability removes whole classes of bugs.