Java records provide a concise way to represent related values. A small declaration can replace much of the repetitive code normally needed for a data-carrying class.
That convenience can hide an important design question: what happens when a record contains a mutable object? A record’s components cannot simply be reassigned after construction, but that does not automatically make every referenced object immutable.
Understand the shallow guarantee
Oracle describes records as shallowly immutable data carriers. Each component corresponds to a private final field and a public accessor. The compiler can also supply the canonical constructor and implementations of equals, hashCode, and toString.
Consider this declaration:
record ReadingList(String name, List<String> titles) {}
The record holds a reference to a list. If the supplied list remains mutable and another part of the application changes it, the record can expose a different collection of titles later.
The declaration is compact, but the ownership policy is still a developer decision.
Establish ownership during construction
For a record intended to represent a stable reading list, a compact constructor can capture an unmodifiable list:
import java.util.List;
record ReadingList(String name, List<String> titles) {
ReadingList {
titles = List.copyOf(titles);
}
}
List.copyOf produces an unmodifiable list containing the supplied elements. Later structural changes to the original collection are not reflected in the returned list. It also rejects a null collection or null elements.
In this example, the elements are strings. That choice makes the intended value model relatively simple.
Document the null policy instead of allowing callers to discover it accidentally. An empty reading list and a missing list may have different meanings in the application.
Examine the elements as well
Replacing List<String> with List<MutableBook> changes the analysis. Preventing additions and removals does not prevent the book objects themselves from changing.
Ask what the record is meant to capture. Is it a historical snapshot of titles at a specific moment, or a view of current book objects that can be updated elsewhere?
For a snapshot, consider storing immutable values representing the information required by the consumer. For a live view, make that behavior clear in the type’s name and documentation.
Avoid calling a structure a snapshot when changes elsewhere can still alter what it represents.
Test the boundaries that matter
A useful test begins by constructing the record from a mutable source list. Change the source list afterward and check whether the record still represents the original collection.
Next, examine the accessor’s result. If the contract promises an unmodifiable collection, attempts to change its structure should not silently succeed.
Finally, test any mutable element types separately. This last step catches a common oversight: the collection boundary is protected, but the objects inside remain shared.
These tests should verify the intended contract rather than merely repeat the constructor’s implementation.
Choose records for clear value models
Records work particularly well when their components communicate a coherent set of values. They do not remove the need to decide who owns mutable data.
Before adopting a record, write one sentence describing whether its state should remain stable after construction. Then ensure that the component types, constructor, accessors, and tests support that promise. Concise syntax is most useful when the underlying contract is equally clear.
Sources:

