CKTIS / Java
Deep immutability and defensive copying
A `final` field does not freeze an array. Copying mutable data on both input and output keeps shared references from changing an object's state.
Version: Java SE 21
Syntax
Java SE 21 design pattern, not a language construct: `private final int[] values;` with `this.values = input.clone();` in the constructor and `return values.clone();` in the accessor. This isolates a one-dimensional primitive array. For nested mutable state, copy or convert each mutable part to an immutable value. `List.copyOf(source)` takes an unmodifiable snapshot of list membership and order; `Collections.unmodifiableList(source)` provides an unmodifiable view of the backing list. Neither copies the element objects.Deep immutability and defensive copying address two related questions: what can change, and who can change it? A deeply immutable object's logical state cannot change after construction, including through nested objects. A final reference field cannot be reassigned, but the array it references can still be edited. A record's default accessor likewise does not copy a mutable component.
Make defensive copies at both boundaries. On input, copy mutable data before storing it so changes to the argument cannot affect the object. On output, return a copy or an immutable value instead of exposing a mutable internal object. Keep the internal copy private and do not mutate it after construction.
For int[], clone() is enough because the elements are primitive values. For int[][], clone() creates only a new outer array; the inner arrays are still shared. An object-array copy likewise retains element references. Copy the relevant mutable parts or convert them to immutable values.
Collections.unmodifiableList(source) gives an unmodifiable view that reflects changes to the backing list. List.copyOf(source) gives an unmodifiable snapshot of the elements and their order, but does not copy the element objects. Mutable elements can still change in either case, and mutating the returned list itself throws UnsupportedOperationException. List.copyOf throws NullPointerException if the source or any element is null.
Examples
public final class IntSnapshot {
private final int[] values;
public IntSnapshot(int[] input) {
this.values = input.clone();
}
public int[] values() {
return values.clone();
}
public static void main(String[] args) {
int[] input = {7};
IntSnapshot snapshot = new IntSnapshot(input);
input[0] = 99;
System.out.println(snapshot.values()[0]);
int[] returned = snapshot.values();
returned[0] = 42;
System.out.println(snapshot.values()[0]);
}
}Result
7 7
Expect `7` twice. The program first changes the input array element to `99`, then changes the returned array element to `42`. Neither write changes the state of `IntSnapshot`.
Related lessons
- Java · Lesson 1 · Advanced · Version: 2
- Java · Lesson 2 · Advanced · Version: 2
Sources
- Java Language Specification, Java SE 21, §4.12.4 — final Variables
- Java Language Specification, Java SE 21, §10.7 — Array Members
- The Java Tutorials — A Strategy for Defining Immutable Objects
- Java SE 21 API — List.copyOf(Collection)
- Java SE 21 API — Collections.unmodifiableList(List)
- Java SE 21 API — Record