liveBook · Manning

Mutable reference

As explained in Data-Oriented Programming in Java

A mutable reference is a reference that provides access to an object whose state can be changed.

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

How Data-Oriented Programming in Java covers it

How deep it goes
Demonstrates externally visible mutation, hash-collection consequences, defensive copying, immutable wrappers, and encapsulation failures.
Examples
Uses mutable Date and List fields, Person records, HashSet behavior, Cart entities, defensive copies, and immutable collection wrappers.
What it assumes
Assumes familiarity with Java references, records, collections, equality, hashing, and object encapsulation.
What you can do after
Diagnose shared-state hazards and apply defensive copying or immutable collection types at construction boundaries.
How it connects
Connects mutable references to value objects, records, immutability, identities, encapsulation, equality, hashing, and invariants.

Also explained in

Compare how 5 books explain mutable reference →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.