Scrivio Interview library
ConceptFoundations8 min read · updated 12 November 2026

Compare a Java record with a Lombok @Value class. When would you still choose Lombok?

A record is a Java language feature that gives us a final class with final fields, a canonical constructor, accessors named after the components (name(), not getName()), and equals, hashCode and toString. Lombok's @Value generates almost the same class at compile time, but with JavaBean getters, optional builders and withers, inheritance from an ordinary superclass, and support for old Java versions. We still choose Lombok when we need those extras, and for JPA entities, which can never be records.

Why this question is asked

Most Java services are full of small classes that only carry data: request bodies, responses, events, value objects such as Money. For years Lombok was the usual way to avoid writing their boilerplate by hand. Since Java 16, records do the same job inside the language, so interviewers ask this question to see whether we know what each one really generates and whether we can pick the right tool instead of following habit. In this answer, we will recap what a record is, compare it line by line with a Lombok @Value class, look at the limits of records, and then decide when Lombok is still the better choice. We will finish with validation in a record's constructor.

1A Quick Recap of Records and @Value

A record is a special kind of class for plain data. We declare the components, the values it carries, in the header, and the compiler writes the rest: a private final field for each component, a public constructor that takes all of them in order (the canonical constructor), one accessor method per component, and equals, hashCode and toString based on all components. The class is implicitly final and implicitly extends java.lang.Record.

Lombok is an annotation processor, a plugin that runs inside the compiler and adds code to our classes before they are compiled to bytecode. Its @Value annotation turns an ordinary class into an immutable value class: it makes the class final, makes every field private and final, adds a constructor for all fields, a getter for each field, and equals, hashCode and toString.

Java
record Customer(String id, String name) {}
@lombok.Value
class CustomerValue { String id; String name; }
var r = new Customer("c1", "Asha");
var v = new CustomerValue("c1", "Asha");
r.name(); // "Asha"
v.getName(); // "Asha"
r.equals(new Customer("c1", "Asha")); // true
r.toString(); // Customer[id=c1, name=Asha]
v.toString(); // CustomerValue(id=c1, name=Asha)

Notice that both classes behave as values: two instances with the same data are equal. The visible differences are the accessor names and the toString format. Those small differences matter more than they look, because frameworks, templates and our own code call those methods by name.

2What Records Cannot Do

Records are deliberately narrow. A record cannot extend another class, because it already extends java.lang.Record; it can implement interfaces, which is how we share behaviour between records. A record cannot declare extra instance fields outside its components. It may declare static fields, static methods, instance methods and extra constructors, as long as every other constructor ends up calling the canonical one.

Records are also only shallowly immutable. The fields are final, so we cannot point them at a different object, but if a component is a mutable object such as an ArrayList, the list itself can still change.

Java
record Basket(String id, List<String> items) {}
var list = new ArrayList<>(List.of("apple"));
var b = new Basket("b1", list);
list.add("pear");
b.items(); // [apple, pear]: the record did not protect us
record SafeBasket(String id, List<String> items) {
SafeBasket { items = List.copyOf(items); } // defensive copy in the compact constructor
}

The same is true of a Lombok @Value class: final fields do not make the objects they point to immutable. In both cases the fix is a defensive copy, and List.copyOf gives us an unmodifiable copy in one call.

3Comparing the Two Side by Side

The table below puts the main differences next to each other. Most of them come from one fact: a record is part of the language, so every tool that understands Java understands records, while Lombok is extra code generation that every tool has to be taught about through plugins.

Java record vs Lombok @Value
AspectJava recordLombok @Value class
Where it comes fromThe language, Java 16 and laterAnnotation processor, any Java version
Accessor namesname()getName()
InheritanceCannot extend a class; can implement interfacesCan extend a non-final superclass
Extra instance fieldsNot allowedAllowed
Builder, witherOnly with Lombok @Builder / @With on the record@Builder, @With
toString formatCustomer[id=c1, name=Asha]CustomerValue(id=c1, name=Asha)
IDE and tool supportBuilt inNeeds the Lombok plugin
JPA entityNot possibleNot with @Value; use @Getter/@Setter
Both give immutable value classes; the differences are naming, inheritance and extras.

The rows about builders and withers deserve a word. A builder lets us create an object by naming only the fields we want to set, which is much easier to read than a constructor with twelve arguments. A wither, generated by @With, returns a copy of the object with one field changed, such as order.withStatus(SHIPPED). Lombok can generate both on records too, so moving to records does not force us to give them up.

4When We Choose Records

For new code on Java 16 or later, records are the default for data carriers: DTOs (data transfer objects, the classes we receive and send over an API), value objects such as Money or DateRange, events, and the results of queries. The frameworks we use already understand them. Jackson serialises and deserialises records directly, using the canonical constructor. Spring Boot binds @ConfigurationProperties to a record through its constructor, so configuration becomes immutable. Spring Data can return a record as a DTO projection, filling it from the selected columns.

Java
record MailProperties(String host, int port, Duration timeout) {}
// @ConfigurationProperties("app.mail") on the record, bound from:
// app.mail.host=smtp.example.com
// app.mail.port=587
// app.mail.timeout=5s
interface OrderRepository extends JpaRepository<OrderEntity, Long> {
List<OrderSummary> findByCustomerId(String customerId); // a record projection
}
record OrderSummary(Long id, BigDecimal total) {}

So records remove a dependency, need no IDE plugin, and still work across Jackson, Spring Boot and Spring Data.

5When Lombok Is Still the Better Choice

There are three situations where Lombok earns its place. The first is an object with many optional fields, such as a search request with fifteen filters. A record's constructor must take all fifteen in order, which is unreadable at the call site, while a builder lets us set only the three we need. We can put Lombok's @Builder on the record itself, or keep a class.

The second is code that must follow JavaBean naming, where the getX() convention is expected: older templates, expression languages, reporting tools or libraries that look for getters by reflection. Records do not have getX() methods, so such code may not find the properties at all. The third is JPA entities. An entity must have a no-argument constructor, must not be final so that Hibernate can create lazy-loading proxies, and has fields that change while it is managed. A record breaks all three rules, so it can never be an entity. Lombok's @Getter and @Setter (not @Value, which is final, and carefully with @EqualsAndHashCode) remain common on entity classes. Records can still serve as embeddables in recent Hibernate versions and as projections around the entities.

Finally, a library that must still run on Java 8 or 11 cannot use records at all, and @Value is the only way to get the same convenience there.

6Adding Validation in a Record's Constructor

A record does not stop us from checking its data. Its compact constructor is a constructor without the parameter list: it runs before the fields are assigned, can check or normalise the parameters, and then the compiler assigns them for us.

Java
record Money(BigDecimal amount, String currency) {
Money {
Objects.requireNonNull(amount, "amount");
if (currency == null || currency.length() != 3)
throw new IllegalArgumentException("currency must be a 3-letter code");
amount = amount.setScale(2, RoundingMode.HALF_UP); // normalise before assignment
}
}
new Money(new BigDecimal("10.5"), "EUR").amount(); // 10.50
new Money(BigDecimal.ONE, "EURO"); // IllegalArgumentException

Because every other constructor must delegate to the canonical one, no record can exist without passing these checks. For request bodies we can also put Bean Validation annotations such as @NotBlank on the components and use @Valid in the controller; Spring then validates the record after Jackson has created it.

7Follow-up questions

How do you add validation to a record's constructor?

We write a compact constructor, which is declared with the record name and no parameter list. Inside it we can check each parameter and throw IllegalArgumentException or NullPointerException, and we can reassign the parameter to normalise it, for example by trimming a string or making a defensive copy of a list. The compiler assigns the fields after the compact constructor finishes, so the checks always run before the record exists. For web input we can also annotate the components with Bean Validation constraints and validate with @Valid.

Can a record have a builder?

Not by itself, but Lombok supports @Builder and @With on records, so we get a fluent builder and copy-with-one-change methods while keeping the record's language guarantees. Without Lombok, we can write a static factory method or a small nested builder class by hand, which is reasonable when only a few records need it.

8Conclusion

In this answer, we saw that a record is a language-level final class with final fields, a canonical constructor, component-named accessors and generated equals, hashCode and toString, while Lombok's @Value generates a similar class with getX() getters, optional builders and withers, inheritance and old-Java support. Records cannot extend classes, cannot add instance fields and are only shallowly immutable. We choose records for DTOs, value objects and payloads, where Jackson, Spring configuration properties and Spring Data projections already support them, and we keep Lombok for big optional-field builders, JavaBean naming and JPA entities.

What to say in the interviewAbout 80 seconds aloud

Open with

A record and a Lombok @Value class both give me an immutable value type, but a record is part of the language. The compiler makes it final with private final fields, a canonical constructor, accessors named after the components, so name() rather than getName(), and equals, hashCode and toString over all components.

Then, if they want more

Records cannot extend a class, cannot have extra instance fields, and are only shallowly immutable, so I copy mutable components with List.copyOf in the compact constructor, which is also where I put validation. Lombok's @Value generates getX() getters, works on any Java version, allows a superclass, and pairs with @Builder and @With.

On Java 16 and later I use records by default for DTOs, value objects and API payloads, because Jackson, Spring Boot's configuration properties and Spring Data projections all support them and I remove a dependency.

I still use Lombok when I need a builder for many optional fields, when a library expects JavaBean getters, and for JPA entities, which can never be records because they need a no-argument constructor, a non-final class and mutable state. Lombok can also put @Builder on a record if I want both.

About this answer

Last updated
12 November 2026
Versions it names
Java 16, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "Compare a Java record with a Lombok @Value class. When would you still choose Lombok?": https://scrivio-interview-library.netlify.app/java/lombok/55c13d0c05/