Quick revision
The short answer
@Data generates getters for all fields, setters for non-final fields, a constructor for the required (final or @NonNull) fields, and toString, equals and hashCode over all non-static, non-transient fields. @Value generates an immutable class instead: it makes the class final and every field private final, and adds getters, an all-args constructor, toString, equals and hashCode, with no setters. Lombok does this as an annotation processor that changes the compiler's syntax tree during javac through internal compiler APIs, so the methods exist in the bytecode but never in our source.
What to remember
In this answer, we saw that @Data combines @Getter, @Setter, @RequiredArgsConstructor, @ToString and @EqualsAndHashCode over all non-static, non-transient fields, while @Value creates an immutable class with private final fields, a final class, getters, an all-args constructor and the same three object methods. Lombok produces this code by running as an annotation processor and changing javac's syntax tree through internal compiler APIs, which is why the methods are in the bytecode but not the source, and why JDK upgrades can require a Lombok upgrade. The finer annotations let us make each decision deliberately.
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.
@Data generates getters for all fields, setters for non-final fields, a constructor for the required (final or @NonNull) fields, and toString, equals and hashCode over all non-static, non-transient fields. @Value generates an immutable class instead: it makes the class final and every field private final, and adds getters, an all-args constructor, toString, equals and hashCode, with no setters. Lombok does this as an annotation processor that changes the compiler's syntax tree during javac through internal compiler APIs, so the methods exist in the bytecode but never in our source.
Why this question is asked
Lombok is in a large share of Spring codebases, and @Data and @Value are its most used annotations. Interviewers ask what they generate because many bugs come from code we never see: an equals that includes a mutable field, a toString that prints a password, or a setter we did not want. They also ask how Lombok works at all, because the answer explains why a JDK upgrade can break the build. In this answer, we will recap what Lombok is, see exactly what @Data and @Value produce, look at how Lombok changes the compiler's work, and finish with the finer-grained annotations we usually prefer.
1A Quick Recap of Lombok
Lombok is a library that writes boilerplate code for us at compile time. Instead of typing getters, setters, constructors, equals, hashCode and toString, we put an annotation on the class and Lombok adds those methods while the class is compiled. It is a compile-time tool only: our running application does not need the Lombok jar.
2What @Data Generates
@Data is a shortcut for five annotations together: @Getter, @Setter, @RequiredArgsConstructor, @ToString and @EqualsAndHashCode. Let us apply it to a small class and look at what we can call afterwards.
Notice the details. The required-args constructor takes only fields that must be set: final fields without an initialiser and fields marked @NonNull. Non-final fields get setters, final fields do not. toString includes every field. equals and hashCode use all non-static, non-transient fields, which is why visits is left out of the comparison. Lombok also generates a canEqual method so that equality stays symmetric when subclasses are involved.
3What @Value Generates
@Value is the immutable version. It makes the class final, makes every field private and final, and generates getters, an all-args constructor, toString, equals and hashCode. There are no setters, because nothing can change after construction.
| Generated | @Data | @Value |
|---|---|---|
| Getters | all fields | all fields |
| Setters | non-final fields | none |
| Constructor | required args (final, @NonNull) | all args |
| Fields | as written | made private final |
| Class | as written | made final |
toString, equals, hashCode | yes, over all non-static, non-transient fields | yes, same fields |
This is close to what a Java record gives us today. A record has accessors named cents() instead of getCents(), and it cannot extend another class, but it needs no library. For new immutable data carriers on current Java, a record is often the simpler choice.
4How Lombok Generates Code Without Macros
Java has no macros, and the official annotation processing API (JSR 269) only lets a processor read the code and generate new files; it cannot add methods to an existing class. Lombok goes further. It runs as an annotation processor inside javac, and when it is called, it reaches into javac's internal classes, the com.sun.tools.javac packages, and modifies the abstract syntax tree, the tree of nodes the compiler builds from our source. It adds method nodes for getters, constructors and so on, and then lets javac carry on as if we had written them.
- javac parses the sourcebuilds the abstract syntax tree
- Annotation processors runLombok is one of them
- Lombok edits the treeadds getters, constructors, equals through internal javac APIs
- javac checks typesthe new methods are visible to other code
- javac writes bytecodethe .class file contains the generated methods
5 steps in order. Play them, or step through them one at a time.
So the methods end up in the .class file, other code can call them, and our .java file stays short. But there is a catch: these internal compiler classes are not a public API. They change between JDK versions and are protected by the module system's strong encapsulation. That is why a new JDK release sometimes needs a new Lombok release before the build works again, and why Lombok should be upgraded together with the JDK.
5Finer Control: @Getter, @Setter, @ToString and @EqualsAndHashCode
@Data is convenient but makes many decisions at once. The individual annotations let us choose. We can put @Getter on the class and @Setter only on the fields that may change, exclude a field from toString, or limit equality to an id.
This is usually a better default than @Data, because each generated method is there on purpose, and the reader can see which fields take part in equality and which are hidden from logs.
6Follow-up questions
How do you see the code Lombok generates (delombok)?
Lombok ships a tool called delombok that writes plain Java source with all the generated code expanded. We can run java -jar lombok.jar delombok src/main/java -d target/delombok, use the lombok-maven-plugin or a Gradle task, or in IntelliJ IDEA use Refactor, Delombok on a class. For a quick look at the result without changing sources, javap -p on the compiled class lists every method Lombok added.
Why does @Value make the class final?
So that nobody can create a subclass that adds mutable state or overrides equals, which would break the promise of immutability and of value-based equality. If we really need a subclass, we can add @NonFinal to the class, but that is a sign the class may not be a value.
Does @Data work with records?
No and it is not needed. A record already has accessors, a canonical constructor, equals, hashCode and toString. Lombok annotations such as @Builder can be used on records, but @Data, @Value and @Setter cannot.
7Conclusion
In this answer, we saw that @Data combines @Getter, @Setter, @RequiredArgsConstructor, @ToString and @EqualsAndHashCode over all non-static, non-transient fields, while @Value creates an immutable class with private final fields, a final class, getters, an all-args constructor and the same three object methods. Lombok produces this code by running as an annotation processor and changing javac's syntax tree through internal compiler APIs, which is why the methods are in the bytecode but not the source, and why JDK upgrades can require a Lombok upgrade. The finer annotations let us make each decision deliberately.
Open with
@Data is shorthand for @Getter, @Setter, @RequiredArgsConstructor, @ToString and @EqualsAndHashCode. So we get getters for every field, setters for non-final fields, a constructor that takes the final and @NonNull fields, and toString, equals and hashCode over all non-static, non-transient fields. @Value is the immutable version: it makes the class final and every field private final, and generates getters, an all-args constructor, toString, equals and hashCode, with no setters.
Then, if they want more
On current Java a record often replaces @Value. As for how it works: Java has no macros, and the standard annotation processing API can only generate new files, not change existing classes.
Lombok runs as an annotation processor inside javac but uses javac's internal classes to modify the abstract syntax tree directly, adding the method nodes before type checking and bytecode generation. So the methods exist in the class file but not in the source.
The catch is that those internals are not a public API, so a new JDK can break an older Lombok version, and we upgrade them together.
In practice I prefer the finer annotations, like @Getter and @ToString.Exclude on a password field, so each generated method is a deliberate choice, and I use delombok or javap when I want to see the generated code.
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 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 "What code does Lombok generate for @Data and for @Value? How does Lombok generate code...": https://scrivio-interview-library.netlify.app/java/lombok/546c26baf1/