Scrivio Interview library
ConceptPrincipal6 min read · updated 12 November 2026

Records were described as data carriers with transparent state rather than as a way to reduce boilerplate. Why does that distinction matter, and how should it guide where records are allowed in an organization's architecture?

Records were designed as transparent carriers of data: a record's public API is exactly its state, which is what makes reliable equals, construction and deconstruction in record patterns possible. Shorter code is a side effect. So an organization should allow records where a type's role is to be data, such as DTOs, events, messages, value objects and sealed algebraic data types, and keep ordinary classes for objects that protect invariants behind behaviour.

Why this question is asked

Many teams adopted records because they replace fifty lines of getters and equals with one. That works for a while, then someone adds a cache field workaround, hides a component behind a different accessor, or turns a record into a service with dependencies, and the code starts fighting the language. Interviewers at this level ask the question to see whether we can explain the design intent and turn it into guidance that holds across many teams. In this answer, we will recap what transparent state means, see what that guarantee buys the compiler and the runtime, look at why treating records as a boilerplate reducer goes wrong, and then write guidance tied to roles in the architecture rather than to syntax.

1What Transparent State Means

When the Java designers introduced records in JEP 395, they described them as nominal tuples: a named, typed group of values whose meaning is the values themselves. Transparent means that nothing about the state is hidden. Every component is visible through an accessor, the canonical constructor takes exactly the components, and equals, hashCode and toString are derived from all of them. If we know a record's components, we know everything about the object.

Compare that with a typical encapsulated class, an Account that hides its balance field, exposes deposit() and withdraw(), and guarantees that the balance never drops below zero. Its API is behaviour, and its representation can change freely as long as the behaviour stays the same. A record makes the opposite promise: the representation is the API.

Java
record Money(BigDecimal amount, Currency currency) {}
Money m = new Money(new BigDecimal("12.50"), Currency.getInstance("EUR"));
// construct from the parts, read the parts back, compare by the parts
System.out.println(m.amount()); // 12.50
System.out.println(m.equals(new Money(new BigDecimal("12.50"), m.currency()))); // true

2What the Guarantee Buys Us

Because state and API are the same thing, the language can safely do things with records that it cannot do with arbitrary classes. The clearest example is the record pattern, final since Java 21. A pattern can take a record apart into its components, because the compiler knows the components and their accessors and knows that constructing from those values and taking them apart again gives back an equal object.

Java
sealed interface Shape permits Circle, Rect {}
record Circle(double r) implements Shape {}
record Rect(double w, double h) implements Shape {}
static double area(Shape s) {
return switch (s) {
case Circle(double r) -> Math.PI * r * r;
case Rect(double w, double h) -> w * h;
};
}
System.out.println(area(new Rect(2, 3))); // 6.0

The same guarantee lets serialization rebuild records through the canonical constructor, lets Jackson bind JSON to records without setters, and is part of what prepares records for value classes, which Project Valhalla is developing for future JDKs. None of this would be safe if a record could carry hidden state, because deconstructing and reconstructing would silently lose it.

3Why Records Used as Boilerplate Reducers Go Wrong

Problems start when a team sees records only as a shorter class. Typical examples are a record that stores a mutable list and mutates it through methods, a record with a component that is really an implementation detail, such as a cached lookup map, a record whose accessor is overridden to return something other than the field, and a record used as a Spring service holding repositories as components. Each one compiles, and each one breaks a promise that other code relies on.

Roles and whether a record fits
RoleRecord?Reason
Request and response DTOYesThe data is the contract
Domain or integration eventYesImmutable fact, compared by value
Value object (Money, Email)YesIdentity is the values; local checks only
Variant of a sealed result typeYesEnables exhaustive record patterns
JPA entityNoNeeds identity, mutation and proxies
Aggregate with hidden invariantsNoAPI should be behaviour, not state
Spring service or componentNoDependencies are not data
Decide by the role a type plays, not by how much code it saves.

Take the cached map. It becomes part of equals and hashCode, so two logically equal objects compare as different once one has filled its cache. It appears in toString and in JSON. And a record pattern will hand it out as if it were data. The language did not fail here; the type was given the wrong tool. Lombok-style annotations would have been the right fit for a concise class with hidden parts, or a plain class with the boilerplate written by hand.

4Turning the Intent Into Organization Guidance

Guidance written in terms of syntax, such as use records for classes with only getters, ages badly, because almost any class can be squeezed into that shape. Guidance written in terms of roles survives. I would write it as a short rule: a record is allowed when the type's role is to be data that crosses a boundary or is compared by value, meaning messages, DTOs, events, configuration values, value objects and the variants of sealed result or command types. A class is required when the type owns behaviour that protects an invariant, has identity across changes, or hides its representation.

The rule then needs support around it. Architecture tests, for example with ArchUnit, can check that packages named api, events or messages contain records, and that nothing annotated as a JPA entity or a Spring component is a record (the second one the compiler already enforces for entities, since records are final). Code review checklists should flag records with mutable components and records that override accessors to return something different. Starters and templates should generate records for new DTOs so the default is right.

Notice that the guidance also says where records are not the answer to brevity. If a class with hidden state is tedious to write, the answer is a class plus IDE generation or Lombok, not a record with workarounds. That keeps the semantic promise intact, which matters more as record patterns and sealed types spread through the codebase.

5Follow-up questions

Is it wrong to add methods to a record?

No. Records can have static factories, derived methods such as Money.plus(Money), and validation in the compact constructor. What fights the design is adding behaviour that depends on hidden state or that changes the record, because the record's API should stay a faithful view of its components.

How do sealed interfaces relate to this?

A sealed interface whose permitted subtypes are records is an algebraic data type: a closed set of variants, each a plain group of values. The sealing makes switches exhaustive, and the records make each variant deconstructable, so together they model results, commands and events very precisely.

Should we convert existing classes to records to follow the guidance?

Only where the role fits and the change is safe. Records change accessor names, are final and derive equals from every component, so converting public classes can break callers. New types follow the guidance by default; existing ones are converted when they are touched and when compatibility allows.

6Conclusion

In this answer, we saw that records are defined by a semantic promise: their API is their state. That promise is what makes record patterns, constructor-based serialization and framework binding reliable, while the shorter code is only a side effect. Using records as general classes with hidden behaviour breaks that promise, so organization guidance should be written around roles: records for DTOs, events, messages, value objects and sealed variants, classes for encapsulated objects with invariants, backed by architecture tests, review checklists and templates.

What to say in the interviewAbout 90 seconds aloud

Open with

The way I think about it is that records were designed to model data, not to save typing. A record's public API is exactly its components: the canonical constructor takes them, the accessors expose them, and equals, hashCode and toString are derived from all of them.

Then, if they want more

Because there is no hidden state, the language can do things it could not do for normal classes, the main one being record patterns, where a switch can take a record apart into its components and the compiler knows that is the full story.

Serialization rebuilding records through the canonical constructor and Jackson binding without setters rely on the same guarantee. If a team treats records as a boilerplate reducer, they end up with records holding caches, mutable lists or dependencies, and then equals, toString and deconstruction expose things that are not really data.

So for an organization I would not write guidance like use records when a class only has getters. I would tie it to roles: records for request and response DTOs, events and messages, configuration values, value objects like Money or Email, and the variants of sealed result types. Ordinary classes for entities, aggregates that protect invariants behind behaviour, and services.

Then I would back that with ArchUnit rules on the api and events packages, a review checklist item for mutable components, and templates that generate records for new DTOs.

About this answer

Last updated
12 November 2026
Versions it names
Java 21
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 "Records were described as data carriers with transparent state rather than as a way to...": https://scrivio-interview-library.netlify.app/java/records/285a3e5fe6/