Immutable objects
An immutable object can't change after it's created. String, Integer, LocalDate and BigDecimal are all immutable.
Why it's worth it:
- Thread-safe without locks: nothing changes, so nothing can race.
- Safe as
HashMapkeys and in sets, because the hash never changes. - Easier to reason about: nobody can modify it behind your back.
How to make a class immutable:
- Make fields
private finaland set them in the constructor. - Provide no setters; "changes" return a new object, such as
withPrice(…). - Make the class
final, or use a record. - Make defensive copies of mutable inputs and outputs (
List.copyOf), or a caller can still change your list.
Example
public record Cart(String owner, List<String> items) {
public Cart {
items = List.copyOf(items); // defensive copy, unmodifiable
}
public Cart add(String item) { // returns a new Cart
var next = new ArrayList<>(items);
next.add(item);
return new Cart(owner, next);
}
}
List<String> source = new ArrayList<>(List.of("Java book"));
Cart cart = new Cart("Asha", source);
source.add("Hack!"); // doesn't affect cart
Cart bigger = cart.add("Spring book"); // the original stays the same
System.out.println(cart.items()); // [Java book]
// cart.items().add("x"); // UnsupportedOperationExceptionCommon mistake
Returning an internal mutable list from a getter. Callers can then change your object's state; return an unmodifiable copy.
Under the hood
Immutability trades a little allocation for a lot of simplicity. Modern JVMs make short-lived objects cheap, so this rarely matters outside hot loops, where a mutable builder such as StringBuilder is the escape hatch. Collections.unmodifiableList is only a read-only view, and changes to the underlying list still show through; List.copyOf makes a real copy.
Check yourself
Which of these is not immutable?
How this connects
Know these first
Where this leads
You've reached the end of this thread. Try a learning path for what's next.
Part of Crack the Java interview, Upgrade from Java 8 to Java 25.
Was this lesson helpful?
Finished reading? Mark it complete to track your progress.