Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

List cases where you should not use a record, and explain why for each.

Do not use a record for an entity with identity and changing state, such as a JPA entity; for a type that must hide its representation or keep internal state, because records expose every component; for a type that needs to extend a class, because records cannot; for a type whose equality should ignore some fields, such as caches or audit timestamps; or for a type with many optional fields, where a builder reads better than a long canonical constructor.

Why this question is asked

Records are so convenient that teams start reaching for them by reflex, and the first sign of trouble is usually a workaround: an overridden equals that skips a field, an accessor that returns something other than the component, a cache smuggled in through a mutable component. Interviewers ask this question to see whether we know the boundaries of the feature and can explain each one from what records promise. In this answer, we will recap that promise, then go through five cases where a record is the wrong choice, each with an example and what to use instead.

1A Quick Recap of What a Record Promises

A record is a final class whose state is exactly the list of components in its header. The compiler gives it private final fields, a public accessor per component, a canonical constructor taking all components, and equals, hashCode and toString over all components. It cannot extend another class, because it already extends java.lang.Record, and it cannot declare additional instance fields.

Every case below follows from those facts. When a type needs something the promise rules out, using a record means fighting the language, and the workarounds usually make the code worse than a plain class would have been.

When a record is the wrong choice
CaseWhy a record failsUse instead
Entity with identity, changing stateFinal fields, no no-arg constructorMutable class with business methods
Hidden representation or internal stateEvery component is public APIEncapsulated final class
Needs to extend a classRecords cannot extend classesClass hierarchy, or interfaces
Equality on a subset of fieldsequals uses every componentClass with explicit equals
Many optional fieldsLong positional constructorClass with a builder
Each row breaks one part of the record promise.

2Entities With Identity and Changing State

An entity is an object that keeps its identity while its state changes: customer 42 is the same customer after changing address. Records model the opposite, values that are equal when their data is equal and that never change. A JPA entity cannot be a record in any case, because the provider needs a no-argument constructor, assignable fields and a non-final class for lazy proxies.

Java
record Customer(Long id, String address) {}
Customer before = new Customer(42L, "Old Street 1");
Customer after = new Customer(42L, "New Road 9");
System.out.println(before.equals(after)); // false: the same customer is now 'different'

So with a record, the same customer becomes unequal to itself after a change, and sets or maps of customers behave strangely. Use a class whose equality is based on its id, with methods such as moveTo(address) that change state while keeping the object valid.

3Types That Must Hide Their Representation

Encapsulation means callers depend on behaviour, not on how data is stored. Suppose a RateLimiter keeps an array of timestamps internally. As a record, that array would be a component: visible through an accessor, part of the constructor, printed by toString and compared by equals. Any caller could depend on it, and we could never change the storage without breaking them.

The same applies to internal state that is not part of the value at all, such as a lazily computed hash, a memoised result or a connection. Records cannot declare extra instance fields, so the only way to keep such state is to make it a component, which exposes it. Use a final class with private fields and public behaviour.

4Types That Need a Class Hierarchy

Every record implicitly extends java.lang.Record and is final, so it cannot extend a base class and cannot be extended. If the design calls for an abstract base class with shared fields and template methods, such as an AbstractEvent with an id and a timestamp that subclasses inherit, records do not fit.

Java
abstract class AuditedEvent { final Instant at = Instant.now(); }
record Shipped(String orderId) extends AuditedEvent {} // compile error: records cannot extend classes
// Records can implement interfaces, which often covers the need
sealed interface Event permits Shipped, Cancelled { Instant at(); }
record Shipped(String orderId, Instant at) implements Event {}
record Cancelled(String orderId, Instant at, String reason) implements Event {}

Often the hierarchy can be reshaped into an interface implemented by records, as above, with the shared data repeated as components. When it cannot, because real behaviour and state live in the base class, use ordinary classes.

5Types Whose Equality Should Ignore Some Fields

A record's equals compares every component. That is wrong for a type that carries audit fields, such as lastModified, or a cached value, which should not make two otherwise equal objects different. We can override equals and hashCode in a record, but then the record's components and its equality disagree, record patterns still expose the ignored field, and readers must notice the override to understand the type. Overriding equality in a record is a strong hint that it should be a class.

6Types With Many Optional Fields

Records have one canonical constructor with positional parameters. For three or four components that is clear. For a SearchRequest with fifteen components, most of them optional, call sites become rows of nulls and booleans where nobody can see which argument is which, and adding a field breaks every caller.

Java
new SearchRequest("shoes", null, null, 20, 0, null, true, false, null, null, null, "price", null, null, false);
// which argument is the page size? which boolean turns on facets?
SearchRequest req = SearchRequest.builder().query("shoes").pageSize(20).sortBy("price").build();

A class with a builder makes each value named and optional, and new fields do not break callers. A record can be combined with a hand-written builder, and that is fine for medium-sized types, but once most fields are optional the builder is doing all the work and the record adds little.

7Follow-up questions

Is it acceptable to override an accessor in a record?

Yes, if it still returns the component's value, for example returning an unmodifiable view or adding a null check. It is a warning sign if the accessor returns something different from the component, because record patterns and serialization rely on accessors reflecting the state.

Should configuration properties be records?

Usually yes. They are a set of values read once at startup, compared rarely and never changed, which is exactly what records model. Spring Boot binds records through their constructor.

What about records with a very large number of required fields?

Required fields are less of a problem than optional ones, but beyond about seven or eight components it is worth asking whether the record should be split into smaller records that group related values, such as an Address inside a Customer view.

8Conclusion

In this answer, we went through five cases where a record is the wrong tool: entities with identity and changing state, types that must hide their representation or keep internal state, types that need to extend a class, types whose equality should ignore some fields, and types with many optional fields. Each one breaks part of the record promise, final transparent state compared by every component, so a plain class, possibly with a builder, is clearer.

What to say in the interviewAbout 90 seconds aloud

Open with

I would avoid records in five situations, and each follows from what a record is: a final, transparent carrier whose state is all its components. First, entities, objects with identity and changing state.

Then, if they want more

A customer who moves is still the same customer, but two records with different addresses are not equal, and JPA cannot use records anyway, since it needs a no-arg constructor, mutable fields and non-final classes for proxies.

Second, types that must hide their representation or keep internal state like a cache, because every component is exposed through an accessor, printed in toString and used in equals, and records cannot have extra instance fields. Third, types that need a class hierarchy, because records cannot extend a class; sometimes an interface implemented by records works, otherwise I use classes.

Fourth, types whose equality should not use all fields, such as audit timestamps or cached values; I can override equals in a record, but then the components and equality disagree, which is a sign it should be a class.

Fifth, types with many optional fields, where a long positional constructor full of nulls is hard to read and adding a field breaks every caller, so a class with a builder is clearer. Everywhere else, DTOs, events, value objects, configuration and sealed variants, records are a great fit.

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 "List cases where you should not use a record, and explain why for each.": https://scrivio-interview-library.netlify.app/java/records/6de720a54e/