Quick revision
The short answer
The designers wanted a transparent data carrier whose API is its state, because only that guarantee lets the compiler and runtime derive reliable equals, safe deconstruction in record patterns, constructor-based serialization and, later, value-class migration. A general boilerplate reducer like Lombok's annotations generates code but promises nothing about meaning, so none of those features could rely on it. Our guidance should follow the same line: records where the data is the API, classes where we need encapsulation or hidden state, and no tricks to get around the limits.
What to remember
In this answer, we saw that the designers chose a semantic record, a transparent carrier whose API is its state, over a general boilerplate reducer, because only that guarantee lets the compiler and runtime derive equals, support record patterns, rebuild records safely in serialization and prepare for value classes. Each limit protects part of the guarantee. Organization guidance should say records where the data is the API, classes where encapsulation or hidden state is needed, and forbid tricks such as mutable components that bend records into general classes.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The designers wanted a transparent data carrier whose API is its state, because only that guarantee lets the compiler and runtime derive reliable equals, safe deconstruction in record patterns, constructor-based serialization and, later, value-class migration. A general boilerplate reducer like Lombok's annotations generates code but promises nothing about meaning, so none of those features could rely on it. Our guidance should follow the same line: records where the data is the API, classes where we need encapsulation or hidden state, and no tricks to get around the limits.
Why this question is asked
Every few months someone asks why records cannot have a private field, why they cannot extend a base class, or why the accessors cannot be hidden. The limits feel arbitrary until we see what they buy. Interviewers at this level ask the question because explaining a language design decision, and turning it into rules for hundreds of engineers, is part of the job. In this answer, we will look at the two designs the language team could have chosen, see what each limit protects, look at the features that depend on them, and write guidance that respects them.
1Two Possible Designs
The first possible design was a boilerplate reducer: let any class ask the compiler to generate constructors, getters, equals and toString, with options to exclude fields, rename accessors, add builders and so on. This is roughly what Lombok offers through annotations, and what some other languages offer through flexible data classes. It is convenient, but the generated class can be anything, so the compiler knows nothing more about it than about a hand-written class.
The second design, the one Java chose in JEP 395, was semantic: a record is a nominal tuple, a named group of values, and its whole state is declared in its header. The language does not just generate code for it; it guarantees properties about it. The limits are the price of the guarantees.
| Limit | Guarantee it protects | Feature that relies on it |
|---|---|---|
| No extra instance fields | State is exactly the components | equals, hashCode, toString, patterns |
| Accessors expose every component | Construct and deconstruct are symmetric | Record patterns |
| Canonical constructor required | All creation goes through one gate | Serialization, Jackson binding |
| Final, no extension | No subclass can add hidden state | Exhaustive sealed hierarchies |
| Final fields | Values do not change | Value classes in the future |
2What the Guarantee Enables Today
Because the components are the full state, the language knows that taking a record apart into its components and building it again with the canonical constructor gives an equal object. That symmetry is what makes record patterns safe. A pattern such as case Point(int x, int y) can bind the components, and nested patterns can go deeper, because there is no hidden part the pattern could miss.
The same guarantee lets serialization rebuild records through the canonical constructor instead of filling fields behind the constructor's back, lets frameworks read Class.getRecordComponents() and bind without guesswork, and makes the generated equals and hashCode correct by construction. With a general boilerplate reducer, none of these could be offered, because a class with generated getters might still have an extra field or a subclass that changes behaviour.
3What It Enables Later
The designers also had future features in view. Project Valhalla is developing value classes, objects without identity that the JVM can store flat in memory. A type without identity must be immutable and compared by its state, which records already are, so records are natural candidates to become value records with little or no source change. Another proposed feature, derived record creation, would let us write a copy of a record with one component changed, and it depends on the canonical constructor and accessors matching exactly. Both are still in development, so check their status for your JDK, but they show that the limits were chosen to keep doors open, not to close them.
4Workarounds That Defeat the Design
Because the limits can be inconvenient, teams invent tricks. A mutable component, such as a one-element array or an AtomicReference, used to sneak changeable state into a record. A component that is really a cache, filled lazily. An accessor overridden to return something other than the component. A static map keyed by record instances to attach extra data. Each one compiles.
The trick quietly breaks the guarantees: equality depends on object identity again, a record pattern hands out the mutable reference, and the type stops being a value. In every such case, the honest answer is a class. If writing that class is tedious, use the IDE or Lombok for the boilerplate, but do not bend a record into it.
5Guidance for the Organization
I would write the guidance as one principle and a few rules. The principle: use a record when the data is the API, meaning the type's whole purpose is to carry a fixed set of values, such as DTOs, events, messages, configuration values, value objects and variants of sealed types. Use a class when we need encapsulation: hidden representation, internal state, identity over time, inheritance or custom equality.
The rules: records hold immutable components or defensive copies, made in the compact constructor; accessors return the component, possibly as an unmodifiable view, and nothing else; equals and hashCode are not overridden to ignore components; and mutable holders as components are not allowed. These can be checked in review and partly with ArchUnit. The aim is that whenever an engineer sees a record anywhere in the codebase, the guarantees the language makes are actually true for it.
6Follow-up questions
Wasn't reducing boilerplate the main reason developers wanted records?
It was the most visible request, and records do reduce boilerplate. But the language team chose to deliver it as a consequence of a semantic feature rather than as the goal, because a semantic feature can support patterns, serialization and value classes, while a code generator can only save typing.
Why not allow private extra fields as long as they are excluded from equals?
Because then the record's state would no longer be its components, and deconstruction followed by reconstruction would lose those fields. Every feature built on the symmetry would need exceptions, and the guarantees would become conditions to check rather than facts.
Is using records with sealed interfaces part of the same design?
Yes. Sealed interfaces close the set of variants, and records make each variant transparent, so a switch can be exhaustive over the variants and deconstruct each one fully. Together they give Java algebraic data types, which is a large part of why both features arrived together in the Java 16 to 21 period.
7Conclusion
In this answer, we saw that the designers chose a semantic record, a transparent carrier whose API is its state, over a general boilerplate reducer, because only that guarantee lets the compiler and runtime derive equals, support record patterns, rebuild records safely in serialization and prepare for value classes. Each limit protects part of the guarantee. Organization guidance should say records where the data is the API, classes where encapsulation or hidden state is needed, and forbid tricks such as mutable components that bend records into general classes.
Open with
The designers could have built a general boilerplate reducer, something like Lombok annotations in the language, that generates getters and equals for any class with lots of options.
Then, if they want more
They chose a semantic feature instead: a record is a transparent carrier of data whose API is its state. That gives the compiler and the runtime a guarantee they can build on. Because there are no extra instance fields and every component has an accessor, deconstructing a record and reconstructing it with the canonical constructor gives an equal object.
That symmetry is what makes record patterns safe, including nested ones in switch. It is also why serialization can rebuild records only through the constructor, why Jackson can bind them reliably, and why equals and hashCode are right by construction. The final and immutable parts also prepare records for value classes from Project Valhalla.
A boilerplate reducer could never give these guarantees, because a generated class might still have hidden fields or subclasses. For the organization, I would turn that into a simple principle: use records where the data is the API, DTOs, events, messages, value objects, sealed variants, and use classes where we need encapsulation, hidden state, identity or inheritance.
And I would explicitly forbid workarounds such as AtomicReference or array components to sneak in mutable state, or accessors that return something other than the component, because they compile but break exactly the guarantees that make records worth having.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- 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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Records were deliberately limited: no extension, no extra instance fields and accessors...": https://scrivio-interview-library.netlify.app/java/records/fec42a51f2/