Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

This record is meant to be immutable: record Team(String name, List<String> members) {}. A caller does team.members().add("x") and another thread sees the change. Why, and how do you fix it?

A record makes its fields final, but it stores the exact list object it was given and its accessor returns that same object, so everyone holding a reference to the list can change it, and other threads see the change. The fix is a compact constructor that replaces the component with an unmodifiable copy: members = List.copyOf(members). After that, outside changes no longer reach the record, and members().add("x") throws UnsupportedOperationException.

Why this question is asked

Records were added to make immutable data carriers short to write, and many developers assume that a record is immutable by definition. This question shows where that assumption breaks, and it is a bug we meet in real code when a record is shared between threads or cached. Interviewers use it to check whether we understand the difference between a final reference and an unchangeable object. In this answer, we will recap what a record generates, reproduce the problem, see exactly why it happens, apply the fix with a compact constructor, and then look at what the fix changes for nulls.

1A Quick Recap of What a Record Generates

A record is a class whose purpose is to carry data. From the header record Team(String name, List<String> members) the compiler generates a private final field for each component, a canonical constructor that assigns them, an accessor method for each (name() and members()), and equals, hashCode and toString based on the components. The record class is also implicitly final.

So a record follows several of the rules for immutable classes automatically. But the generated constructor simply assigns this.members = members, and the generated accessor simply returns this.members. Neither of them copies anything.

Java
record Team(String name, List<String> members) {}
// what the compiler writes for us, roughly:
// private final List<String> members;
// Team(String name, List<String> members) { this.name = name; this.members = members; }
// public List<String> members() { return this.members; }

2Reproducing the Problem

Here is the situation from the question. The caller creates a team from an ArrayList, shares the record with another thread, and then adds a member through the accessor.

Java
List<String> list = new ArrayList<>(List.of("ana", "bo"));
Team team = new Team("core", list);
team.members().add("x"); // works: members() returns the ArrayList itself
System.out.println(team.members()); // [ana, bo, x]
list.remove("ana"); // the original reference changes the record too
System.out.println(team.members()); // [bo, x]
System.out.println(team.equals(new Team("core", List.of("ana", "bo")))); // false: equality changed

Notice that the record's equality and hashCode change as well, because they are computed from the list's contents. A Team used as a HashMap key or stored in a HashSet before the change can no longer be found after it. And because ArrayList is not thread-safe, another thread reading the list during the add can see a stale size, a missing element or even a ConcurrentModificationException while iterating.

3Why the Change Is Visible Everywhere

The field members is final, but final only means the field always points to the same object. It says nothing about the object itself. The caller's variable list, the record's field and the value returned by members() are three references to one ArrayList in memory. A change made through any of them is a change to that single list, so every holder sees it.

Who refers to the list
ReferencePoints toCan it change the list?
caller's variable listArrayList at one addressyes
record field members (final)the same ArrayListthe field cannot be reassigned, the list can change
team.members() resultthe same ArrayListyes
another thread's team.members()the same ArrayListyes, and without synchronisation
One list, several references: final protects the reference, not the contents.

4The Fix: Copy in a Compact Constructor

A compact constructor is a special form of constructor that records allow. It has no parameter list; it runs before the fields are assigned, and inside it the parameter names refer to the incoming values, which we can validate or replace. Whatever value the parameter has at the end is what gets stored in the field.

Java
record Team(String name, List<String> members) {
Team {
Objects.requireNonNull(name, "name");
members = List.copyOf(members); // replaces the parameter; the field gets the copy
}
}
List<String> list = new ArrayList<>(List.of("ana", "bo"));
Team team = new Team("core", list);
list.add("zed"); // does not affect the team
System.out.println(team.members()); // [ana, bo]
team.members().add("x"); // throws UnsupportedOperationException

Two things changed. First, List.copyOf made a new list holding the same elements, so the caller's ArrayList and the record no longer share anything. Second, the copy is unmodifiable, so the accessor can return it directly: callers can read it, iterate it and pass it on, but add, remove and clear throw UnsupportedOperationException. We did not need to write a custom accessor at all.

Because the copy can never change, it is also safe to share between threads without locks, and the record's equals and hashCode stay stable for its whole life. The elements here are Strings, which are immutable themselves, so the record is now fully immutable. If the elements were mutable objects, we would need immutable element types as well.

5What the Fix Means for Nulls

List.copyOf, like List.of, does not accept null elements, and it does not accept a null list either. So the fix also changes which inputs the record accepts. That is often what we want, because a null member name is usually a bug, but it is a behaviour change that callers may notice.

Java
new Team("core", Arrays.asList("ana", null)); // NullPointerException from List.copyOf
new Team("core", null); // NullPointerException: the list itself is null

If the record should accept a missing list and treat it as empty, we say so explicitly: members = members == null ? List.of() : List.copyOf(members). If null elements must really be allowed, we cannot use copyOf; we copy into a new ArrayList and wrap it with Collections.unmodifiableList, and because nothing else holds that ArrayList, the wrapper is effectively immutable.

6Follow-up questions

What happens if members is null?

With the fix in place, List.copyOf(null) throws NullPointerException, so a Team can never be created with a null list. Without the fix, the record would happily store null and fail later, when someone calls members().size(). Failing in the constructor is better, because the stack trace points at the code that passed the bad value. If an absent list is a valid input, the compact constructor can turn it into List.of() instead.

Could we override the accessor to return a copy instead?

We could write public List<String> members() { return List.copyOf(members); }, but it is the weaker fix. The record would still share the caller's original list, so changes made through that reference would still leak in, and equals and hashCode would still change. Copying once on the way in fixes both directions and costs nothing on reads.

Does List.copyOf always allocate a new list?

No. If the argument is already an unmodifiable list created by List.of or List.copyOf, it returns the same instance, because there is nothing to protect against. So passing an already immutable list into the record costs no copy at all.

7Conclusion

In this answer, we saw that a record makes its fields final but does not copy or wrap the objects they refer to, so a list passed to the constructor stays shared with the caller and is returned as-is by the accessor. Any change through any reference is visible to all, including other threads, and it even changes the record's equality. A compact constructor with members = List.copyOf(members) breaks the sharing and stores an unmodifiable list, so the accessor is safe to return directly. The same fix rejects null lists and null elements, which we either accept as a feature or handle explicitly.

What to say in the interviewAbout 80 seconds aloud

Open with

The record looks immutable because its fields are final, but final only protects the reference. The generated constructor stores the very ArrayList the caller passed in, and the generated members() accessor returns that same object.

Then, if they want more

So the caller's variable, the record's field and every result of members() point to one mutable list, and an add through any of them is visible to everyone, including another thread, which can also see it in an inconsistent state because ArrayList is not thread-safe.

It even changes the record's equals and hashCode, so a Team used as a map key can get lost. I would fix it with a compact constructor, members = List.copyOf(members).

That makes a separate copy, so later changes to the caller's list do not reach the record, and the copy is unmodifiable, so the default accessor can return it directly and members().add throws UnsupportedOperationException. As a bonus, List.copyOf returns the same instance if it is given a list that is already immutable.

One thing to be aware of is that copyOf rejects a null list and null elements with a NullPointerException; usually that is what I want, but if null should mean an empty team, I would map it to List.of() explicitly.

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.
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 "This record is meant to be immutable: record Team(String name, List<String> members) {}....": https://scrivio-interview-library.netlify.app/java/immutability/7b2c5f5e08/