Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Are records immutable? Compare them with a hand-written immutable class and with Lombok @Value.

Records are shallowly immutable: their fields are final and they have no setters, but a mutable component such as an ArrayList can still change, so deep immutability needs immutable component types or defensive copies. A hand-written immutable class can hide fields, compute derived state and choose its own equals, at the cost of boilerplate. Lombok @Value generates a similar final class through an annotation processor, while a record is a language feature that the compiler, reflection and the JVM understand as a transparent data carrier.

Why this question is asked

Immutable objects are easier to share between threads, safe as map keys and simpler to reason about, so most teams want more of them. When records arrived, the quick answer to the question are records immutable became yes, and the quick answer to how do they compare with Lombok became they are the same thing built in. Both answers are a little wrong, and interviewers ask the question to find out whether we know where. In this answer, we will see what kind of immutability records give, write the same type three ways, compare what each approach allows, and see why records look ahead to value classes.

1What Kind of Immutability Records Give

An object is immutable when its observable state can never change after construction. A record gets part of the way automatically: each component is stored in a private final field, there are no setters, and the fields cannot even be changed through reflection. That is called shallow immutability, because it protects the references held by the record, not the objects those references point to.

Java
record Basket(String owner, List<String> items) {}
List<String> items = new ArrayList<>(List.of("tea"));
Basket b = new Basket("ada", items);
items.add("milk");
System.out.println(b.items()); // [tea, milk]: the record's state changed
b.items().add("jam");
System.out.println(b); // Basket[owner=ada, items=[tea, milk, jam]]

So a record is fully immutable only when all its components are immutable too, such as String, Integer, LocalDate, BigDecimal, other immutable records, or unmodifiable collections. For mutable components we make a defensive copy in the compact constructor, items = List.copyOf(items), and the record becomes deeply immutable for practical purposes.

2The Same Type Written Three Ways

To compare fairly, here is one value type, a Temperature in degrees Celsius, as a hand-written immutable class, as a Lombok class and as a record.

Java
// 1. Hand-written
public final class Temperature {
private final double celsius;
public Temperature(double celsius) { this.celsius = celsius; }
public double getCelsius() { return celsius; }
public double getFahrenheit() { return celsius * 9 / 5 + 32; } // derived, not stored
@Override public boolean equals(Object o) {
return o instanceof Temperature t && Double.compare(celsius, t.celsius) == 0;
}
@Override public int hashCode() { return Double.hashCode(celsius); }
@Override public String toString() { return celsius + " C"; }
}
// 2. Lombok: a final class with private final fields, getters, equals, hashCode, toString
@Value
public class Temperature { double celsius; }
// 3. Record
public record Temperature(double celsius) {
public double fahrenheit() { return celsius * 9 / 5 + 32; }
}
System.out.println(new Temperature(100).fahrenheit()); // 212.0

The hand-written class is about twenty lines; the other two are one or two. Notice the accessor names: the class and Lombok produce getCelsius(), following the JavaBeans convention, while the record produces celsius(). That difference matters when an existing class is converted, because callers and frameworks that expect getters break.

3What the Hand-Written Class Can Do That the Others Cannot Easily

The hand-written class has full control. It can hide its representation, for example storing kelvin internally while exposing Celsius, and change that later without affecting callers. It can keep internal state that is not part of its API, such as a lazily computed cache. And it can define equals over only some of its fields. The cost is boilerplate that must be kept in sync by hand: add a field and forget to add it to equals, and the class quietly breaks.

A record deliberately gives up that freedom. Every component is visible through an accessor, the canonical constructor takes exactly the components, and equals uses all of them. We can add methods and static fields, but not extra instance fields. That is what makes it a transparent data carrier by design: its state is its API.

4Lombok @Value Versus a Record

@Value is processed by Lombok's annotation processor during compilation. It makes the class final, makes the fields private and final, and generates getters, an all-arguments constructor, equals, hashCode and toString. The resulting class is an ordinary class: the JVM and reflection see nothing special about it. Lombok also offers options records do not, such as @Builder, @With for copies with one field changed, excluding fields from equals, and keeping the getX naming.

Three ways to write an immutable type
AspectHand-writtenLombok @ValueRecord
BoilerplateAll by handGenerated at compile timeGenerated by the language
Accessor namesAny, usually getX()getX()x()
Hidden or extra fieldsYesYesNo extra instance fields
equals over some fieldsYesYes, with exclusionsAll components (or override)
Deep immutabilityDefensive copiesDefensive copiesDefensive copies
Record patternsNoNoYes
Build dependencyNoneAnnotation processorNone
All three are only shallowly immutable unless the components are immutable or copied.

A record is part of the language. The compiler knows its components, so it can support record patterns in switch and instanceof. Reflection knows it too, through Class.isRecord and getRecordComponents, so Jackson, Bean Validation and Hibernate can bind it without guessing. Serialization rebuilds it through its constructor. And it needs no extra build plugin, while Lombok works by hooking into compiler internals and must release a compatible version for each new JDK.

5Why Records Prepare for Value Classes

Project Valhalla is working on value classes for future JDKs: classes whose instances have no identity, so the JVM can store them flat in arrays and fields instead of as separate heap objects. A type without identity must be compared by its state and must not change, which is exactly how we already use records. Code that treats records as values, never synchronizing on them and never comparing them with ==, is well placed to benefit when value records become available. The feature is still in development, so check the JEP status for your JDK before planning around it.

6Follow-up questions

Can a record's field be changed with reflection?

No. Field.setAccessible(true) followed by Field.set on a record field throws IllegalAccessException, unlike final fields in ordinary classes, which reflection can still change in many cases. This is part of what makes records trustworthy as data carriers.

Should we stop using Lombok when we adopt records?

Not entirely. Records cover immutable data carriers well. Lombok is still convenient for mutable classes such as JPA entities, builders for large types, and classes that must keep getX naming. Many teams use records for new DTOs and keep Lombok where those needs remain.

How do we get a modified copy of a record?

We call the constructor with the changed value, often through a small method such as withCelsius(double c) that returns new Temperature(c). Derived record creation, a with expression for records, has been proposed for a future JDK, but check its status before relying on it.

7Conclusion

In this answer, we saw that records are shallowly immutable: final fields and no setters, with deep immutability only when components are immutable or copied in the compact constructor. A hand-written class can hide its representation, keep extra state and choose its own equals, but at the cost of boilerplate. Lombok @Value generates a similar final class with getters through an annotation processor, while records are a language feature known to the compiler, reflection and serialization, transparent by design and a good step towards value classes.

What to say in the interviewAbout 90 seconds aloud

Open with

I would say records are shallowly immutable. Each component is a private final field, there are no setters, and even reflection cannot change record fields. But final only protects the reference, so a record holding an ArrayList can still change through that list.

Then, if they want more

For real immutability the components must be immutable types, or I copy them in the compact constructor with List.copyOf. Compared with a hand-written immutable class, a record gives up some freedom.

The class can hide its representation, keep internal state like a cache, compute derived values and define equals over a subset of fields, but I write and maintain all the boilerplate myself. A record exposes every component through an accessor, uses all components in equals, and cannot have extra instance fields, which is what makes it a transparent data carrier.

Lombok @Value produces something close to the hand-written class: a final class with private final fields, getters named getX, a constructor, equals, hashCode and toString, generated by an annotation processor. The result is an ordinary class, so the JVM and reflection see nothing special, and Lombok has to keep up with each JDK.

Records are a language feature: the compiler supports record patterns on them, reflection exposes their components, and Jackson and Hibernate understand them. They also line up with future value classes from Project Valhalla.

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 "Are records immutable? Compare them with a hand-written immutable class and with Lombok...": https://scrivio-interview-library.netlify.app/java/records/148f074f16/