A mutable reference provides access to an object whose state can change. Even a class with final fields, no setters, and value-style equality can lose value semantics when one field refers to mutable state. section 2.2.4 section 2.6.1
The danger is shared change. A Person record holding a List can change when the original list changes, because both objects retain access to the same mutable reference. The record then appears to change without receiving a new value. section 2.6.2
A record poisoned by a mutable list
List<String> friends = Arrays.asList("Joe", "Jane"); Person person = new Person("Bob", friends); friends.set(0, "Billy"); System.out.println(person); // Person[name=Bob, friends=[Billy, Jane]] Copying the incoming list during construction prevents this external mutation from changing the record.
Mutable references create several related design problems:
- They undermine value objects: one mutable field can convert an otherwise value-like object into an identity with time-dependent behavior. section 2.2.4
- They make invariants unreliable when callers can reach through getters and mutate state several layers away. section 9.2.2
Listing 2.18 Even a single mutable addition prevents us from modeling values
class Person {
final String name;
final Date birthday; #A
public String getName(){return this.name;}
public Date getBirthday(){return this.birthday;}
// equals & hashcode, but no setters. We’re a value object!
}
final Person person = new Person("Bob", new Date()); #B
System.out.println(person);
// [out] Person[name=Bob, birthday=Sun Aug 11 22:42:57 PDT 2024] #C
person.birthday().setTime( #D
new Date().getTime() + TimeUnit.DAYS.toMillis(1) #D
); #D
System.out.println(person);
// [out] Person[name=Bob, birthday=Tue Aug 12 22:42:57 PDT 2024] #E
Inspect the Date example to see how a single mutable reference changes an apparently immutable Person through an accessor. section 2.2.4
Not all mutation is automatically a design failure. Internal mutation can be acceptable; uncontrolled mutation is the deeper problem. The practical test is whether state changes remain inside clear boundaries and whether invariants are enforced rather than remembered informally. section 9.2.2




