Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Your platform shares value objects between threads widely and wants a standard for them. A team uses records such as record Order(String id, List<Line> lines) and assumes they are immutable. Define the standard.

The standard is that a value object shared between threads must be deeply immutable, and a record only guarantees the first level: its fields are final, which safely publishes the references, but an object it points to, such as a List, may still be mutable. So records copy every mutable component in the compact constructor, for example lines = List.copyOf(lines), every element type is itself immutable, arrays are never exposed (copy them or use lists), and the rules go into the review guidelines and are checked with tests or static analysis where possible.

Background to this question

Records made value objects pleasant to write in Java, and many teams now assume that a record is automatically immutable and therefore safe to share. That assumption is half right, and the wrong half produces some of the hardest bugs to trace: an order whose lines change after it was validated, or a cached object that one thread modifies while another reads it. A platform team defining a standard has to close that gap with rules that are short, checkable and backed by tooling. In this answer, we will see exactly what a record guarantees and what it does not, then define the standard rule by rule with code, and finish with how to enforce it.

1What a Record Guarantees

A record such as record Order(String id, List<Line> lines) compiles to a final class with a private final field per component, a canonical constructor, accessors, and equals, hashCode and toString. The fields being final matters for concurrency: under the Java Memory Model's final field guarantee, any thread that obtains a reference to a properly constructed record sees the component references the constructor set, and the objects reachable through them as they were at the end of the constructor, without synchronization.

So a record safely publishes its references. What it does not do is make the referenced objects immutable. If the List inside is an ArrayList that someone still holds, it can change after construction, and changes after construction are outside the guarantee.

2The Gap: A Shallowly Immutable Record

Here is the problem in code.

Java
record Line(String sku, int quantity) { }
record Order(String id, List<Line> lines) { }
List<Line> lines = new ArrayList<>(List.of(new Line("A-1", 2)));
Order order = new Order("o-42", lines);
lines.add(new Line("B-7", 1)); // the caller still holds the list
order.lines().size(); // 2: the "immutable" order changed
order.lines().clear(); // the accessor hands out the same mutable list
order.lines().size(); // 0

The order changed twice after it was created, once through the caller's reference and once through the accessor. In a single thread this breaks invariants, such as an order that was validated with one line now having none. Across threads it is worse: another thread may iterate the list while it is being changed and get a ConcurrentModificationException, or see a partly updated state, because ArrayList has no synchronization and those later writes have no happens-before edge to the reader.

3Rule 1: Copy Mutable Components in the Compact Constructor

The compact constructor, the canonical constructor written without a parameter list, runs before the fields are assigned and can replace the parameters. It is the one place every record instance goes through, so it is where we make defensive copies.

Java
record Order(String id, List<Line> lines, Map<String, String> tags) {
Order {
Objects.requireNonNull(id);
lines = List.copyOf(lines); // unmodifiable copy; rejects null elements
tags = Map.copyOf(tags); // same for maps
}
}
List<Line> src = new ArrayList<>(List.of(new Line("A-1", 2)));
Order order = new Order("o-42", src, Map.of());
src.add(new Line("B-7", 1));
order.lines().size(); // 1: the record has its own copy
order.lines().add(new Line("C-3", 1)); // UnsupportedOperationException

List.copyOf returns an unmodifiable list and, if its argument is already such a list, may return it without copying, so the cost is low when values are passed from one record to another. Notice that it is a real copy, not a view: Collections.unmodifiableList would only wrap the caller's list, and the caller could still change it underneath.

4Rule 2: Elements Must Be Immutable Too

An unmodifiable list of mutable objects is still mutable in the way that matters. If Line were a class with a setQuantity method, anyone holding the order could change a line's quantity. Deep immutability means every object reachable from the record is immutable: the record's components, the elements of its collections, and so on down. In practice, that means element types are records themselves, with the same rules, or well-known immutable JDK types such as String, the boxed primitives, BigDecimal, java.time types and UUID.

Component types and what the standard requires
Component typeAllowed as is?What to do
String, Integer, BigDecimal, UUIDYesNothing; already immutable
java.time types (Instant, LocalDate)YesNothing; already immutable
Another record following the standardYesNothing
List, Set, MapNoList.copyOf, Set.copyOf, Map.copyOf in the constructor
Array (byte[], Line[])NoUse a List, or clone in and clone out
Mutable class (Date, a JPA entity)NoReplace with an immutable type or a record

5Rule 3: No Exposed Arrays

Arrays cannot be made unmodifiable, and records have an extra trap with them: the generated equals and hashCode compare array components by reference, not by contents, so two records with equal byte arrays are not equal. If a component must be an array, for example raw bytes for performance, the record clones it in the compact constructor and overrides the accessor to return a clone, and usually overrides equals and hashCode with Arrays.equals and Arrays.hashCode. In most cases it is simpler to use a List, or a dedicated immutable wrapper type.

Java
record Digest(byte[] bytes) {
Digest { bytes = bytes.clone(); }
@Override public byte[] bytes() { return bytes.clone(); } // never hand out the internal array
@Override public boolean equals(Object o) {
return o instanceof Digest d && Arrays.equals(bytes, d.bytes);
}
@Override public int hashCode() { return Arrays.hashCode(bytes); }
}

6Making the Standard Checkable

A standard only helps if reviewers and tools can apply it. In the review guidelines, the rule is written as one sentence with the examples above: shared value objects are records whose components are immutable types, with collections copied in the compact constructor and no arrays. Reviewers then have a quick checklist for any new record.

Tools take over the mechanical part. An ArchUnit test can check that records in the domain packages have no components of array type or of known mutable types such as java.util.Date, and that every record with a List, Set or Map component declares a constructor; check how your ArchUnit version exposes record components. Error Prone has an @Immutable annotation with a checker that verifies a type is deeply immutable at compile time, which suits a platform where these objects are central. A simple unit test per important record, which mutates the input collection after construction and asserts that the record did not change, catches the most common mistake.

Step throughHow the standard is enforced
  1. Written standarddeeply immutable records, copied collections, no arrays
  2. Review checklistcomponent types, compact constructor, element types
  3. Static checksArchUnit rules, Error Prone @Immutable
  4. Unit testsmutate input after construction, record unchanged

4 layers, from the top down. Step through them one at a time.

7Follow-up questions

Is List.copyOf expensive for large lists?

It copies once at construction, which is linear in the size, and returns the same instance when given a list that is already unmodifiable in this way, so passing records around does not copy again. For a value object shared across threads, a single copy is a small price for never having to think about who else holds the list.

Why not use Collections.unmodifiableList instead?

It returns a read-only view of the original list, not a copy. The caller who passed the list can still modify it, and the record would see the changes. List.copyOf makes an independent unmodifiable copy, which is what immutability requires.

Do deeply immutable records still need safe publication?

No. Because all fields are final, the constructor does not let this escape, and nothing reachable changes afterwards, any thread that obtains a reference sees the complete state, even through a racy publication. That is the main practical benefit of the standard: these objects can be put in caches, sent between threads and stored in static fields without extra reasoning.

8Conclusion

In this answer, we saw that records only guarantee shallow immutability: their final fields safely publish the references, but referenced objects such as a List may still change. The standard closes the gap by copying collections in the compact constructor with List.copyOf and Map.copyOf, requiring all element types to be immutable too, keeping arrays out of records or cloning them in and out, and enforcing the rules with review guidelines, ArchUnit or Error Prone checks and simple mutation tests.

What to say in the interviewAbout 110 seconds aloud

Open with

Records are a great base, but they are only shallowly immutable, and the standard has to close that gap. A record's fields are final, so under the final field guarantee any thread that gets a reference sees the component references the constructor set, without synchronization.

Then, if they want more

But if a component is a mutable List, the caller can still change the list they passed in, and the accessor hands out the same list, so anyone can call clear on it.

So for record Order(String id, List of Line lines), the rules would be: first, copy every collection in the compact constructor, lines = List.copyOf(lines), and the same with Set.copyOf and Map.copyOf. That gives an independent unmodifiable copy, unlike Collections.unmodifiableList, which is just a view.

Second, element types must be immutable too, so Line is itself a record following the same rules, or a known immutable JDK type such as String, BigDecimal or the java.time types. An unmodifiable list of mutable objects is still mutable.

Third, no arrays in records, because arrays cannot be made read-only and record equals compares them by reference; use a List, or clone in and out and override equals and hashCode. With those rules, the object is deeply immutable, so it is safe to share between threads, cache and pass around with no extra publication reasoning.

To enforce it, I would put the rules with examples into the review guidelines, add ArchUnit checks for array and known-mutable component types, consider Error Prone's @Immutable checker for core domain types, and add a simple test per important record that mutates the input after construction and asserts the record did not change.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JLS chapter 17, Threads and Locks
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your platform shares value objects between threads widely and wants a standard for them....": https://scrivio-interview-library.netlify.app/java/java-memory-model/ed8e0096de/