Java / Lombok Interview questions
1. What is Lombok?
Lombok is a Java library that removes boilerplate code — getters, setters, constructors, equals / hashCode , toString , and more — by generating it automatically at compile time from simple annotations, rather than you writing it by hand. import...
2. What is the purpose of Lombok in Java?
Lombok exists to cut down the sheer amount of repetitive, mechanical code that plain Java classes typically require — particularly simple data-holding classes (often called POJOs) that need getters, setters, constructors, and standard Object method overrides just to function correctly...
3. How do you add Lombok to a Maven project?
Lombok is added as a regular dependency, typically scoped as provided since it's only needed at compile time — the generated code becomes part of your compiled classes, so Lombok itself doesn't need to be on the runtime classpath. <dependency>...
4. How do you add Lombok to a Gradle project?
Gradle uses the compileOnly and annotationProcessor configurations together for Lombok — compileOnly makes the annotations available while writing code, and annotationProcessor tells Gradle to actually run Lombok during compilation to generate the boilerplate. dependencies { compileOnly 'or
5. What is @Getter used for?
@Getter generates a standard getter method for a field (or every field, when applied at the class level), following the usual JavaBeans naming convention — getFieldName() for most types, or isFieldName() for a boolean field. public class Person { @Getter...
6. What is @Setter used for?
@Setter generates a standard setter method for a field, following the JavaBeans convention of setFieldName(Type value) , and like @Getter , it can be applied per-field or at the class level to cover every field at once. public class Person...
7. What is @ToString used for?
@ToString generates a toString() override that prints the class name followed by each field's name and value, giving a readable default representation instead of Java's default ClassName@hashcode output. @ToString public class Person { private String name; private int age; }...
8. What is @EqualsAndHashCode used for?
@EqualsAndHashCode generates proper equals() and hashCode() overrides based on the class's fields, following the standard contract Java expects — two objects with equal field values are considered equal and produce the same hash code, which is essential for correct behavior...
9. What is @NoArgsConstructor used for?
@NoArgsConstructor generates a public, no-argument constructor for the class — useful for frameworks that require a default constructor to instantiate objects via reflection, such as JPA entities or JSON deserialization libraries like Jackson. @NoArgsConstructor public class Person { privat
10. What is @AllArgsConstructor used for?
@AllArgsConstructor generates a constructor that takes one parameter for every field in the class, in the order the fields are declared — a quick way to get a fully-populating constructor without writing it by hand. @AllArgsConstructor public class Person {...
11. What is @RequiredArgsConstructor used for?
@RequiredArgsConstructor generates a constructor that includes only the fields Lombok considers "required": final fields, and fields annotated @NonNull that don't already have a default value assigned. @RequiredArgsConstructor public class Person { private final String name; // required: included
12. What is @Data used for?
@Data is a convenience annotation that bundles several others together: @Getter , @Setter , @ToString , @EqualsAndHashCode , and @RequiredArgsConstructor , all applied at once with a single annotation on the class. @Data public class Person { private final String...
13. What is @Value used for?
@Value is the immutable counterpart to @Data : it makes the class and all its fields final by default, generates getters (but no setters), a constructor covering every field, toString() , and equals() / hashCode() — producing a fully immutable...
14. What is @Builder used for?
@Builder generates the builder pattern for a class — a fluent, chainable way to construct an object field by field, particularly useful for classes with many fields (especially optional ones) where a single large constructor call would be hard to...
15. What is @Slf4j used for?
@Slf4j generates a private, static logger field named log , pre-configured for the SLF4J logging facade, saving you from writing the same one-line logger declaration in every class that needs logging. @Slf4j public class OrderService { public void placeOrder(Order o)...
16. What is @NonNull used for?
Placing @NonNull on a field, constructor parameter, or method parameter tells Lombok to generate a null check at the start of the relevant generated (or annotated) method, throwing a NullPointerException immediately with a clear message if the value is null...
17. What is @Cleanup used for?
@Cleanup automatically calls a resource's close() method (or another method you specify) at the end of the enclosing block, functioning as a Lombok-era predecessor to Java's built-in try-with-resources for ensuring resources get released. @Cleanup InputStream in = new FileInputStream( "data.txt"
18. What is @SneakyThrows used for?
@SneakyThrows lets a method throw a checked exception without declaring it in a throws clause and without wrapping it in a try/catch — Lombok generates bytecode that throws the checked exception directly, exploiting the fact that the JVM itself doesn't...
19. What is @Synchronized used for?
@Synchronized is Lombok's safer alternative to Java's built-in synchronized keyword on a method: instead of locking on this (or the class object for a static method), which exposes the lock to any external code that also happens to synchronize on...
20. What is @With used for?
@With generates a "wither" method — a method that returns a new instance of the class with one field changed and every other field copied unchanged from the original, which is the standard pattern for "updating" an immutable object without...
21. Describe how Lombok works under the hood (annotation processing)?
Lombok hooks into the Java compilation process as an annotation processor , a standard extension point the javac compiler supports for running code during compilation that can inspect and modify the program being compiled. Unlike most annotation processors, which are...
22. How do you install the Lombok plugin in an IDE?
Because Lombok's generated methods don't exist in your actual source file, an IDE that only parses the source as written would show errors for calls to getters/setters that "don't exist" — the IDE plugin teaches the IDE's own code analysis...
23. What are the types of Lombok annotations?
Lombok's annotations fall into two broad groups, reflected in their package names. Core (lombok.*) Experimental (lombok.experimental.*) Stable, well-established annotations: @Getter, @Setter, @Data, @Builder, @ToString, @EqualsAndHashCode, and similar.
24. How do you exclude a field from @ToString?
Annotating a specific field with @ToString.Exclude removes just that field from the generated toString() output, while every other field is still included as usual. @ToString public class User { private String username; @ToString ....
25. How do you exclude a field from @EqualsAndHashCode?
@EqualsAndHashCode.Exclude on a field removes it from both the equality comparison and the hash code calculation, while every other (non-excluded) field is still used as normal. @EqualsAndHashCode public class Session { private String userId; @EqualsAndHashCode .
26. What is @Builder.Default used for?
Normally, a field's default field initializer (like private int retries = 3; ) is silently ignored when using @Builder , because the builder constructs the object through its own constructor logic rather than the field's normal initialization path — without...
27. Why does IDE support matter for using Lombok effectively?
Since Lombok's generated methods only exist after annotation processing runs during compilation, an IDE without Lombok awareness sees only the source file as literally written — no getters, no setters, no generated constructor — and will flag every call to...
28. What is the difference between @Data and @Value?
Both are convenience bundles that save you from applying several annotations individually, but they target opposite design intents: @Data produces a mutable class; @Value produces an immutable one. @Data @Value Fields stay mutable by default (non-final unless you mark them...
29. What is the difference between @NoArgsConstructor, @AllArgsConstructor, and @RequiredArgsConstructor?
All three generate a constructor, but they differ in which fields end up as parameters. @NoArgsConstructor @RequiredArgsConstructor @AllArgsConstructor No parameters at all....
30. When should you use @Builder instead of a constructor?
Reach for @Builder once a class has several fields, especially a mix of required and optional ones, or multiple fields of the same type where positional constructor arguments become easy to mix up (two adjacent String parameters, for instance, where...
31. How does @EqualsAndHashCode handle inheritance by default?
By default, @EqualsAndHashCode does not call the superclass's equals / hashCode — it only considers the fields declared in the annotated class itself, which can silently produce incorrect equality if a subclass has meaningful fields inherited from a parent class...
32. Why can @Data be risky on JPA entity classes?
@Data bundles @EqualsAndHashCode and @ToString across every field, which interacts poorly with several JPA/Hibernate specifics: lazy-loaded associations can trigger unwanted database queries the moment toString() or equals() touches them, and including a mutable, database-generated id field in eq
33. How do you combine Lombok's @Builder with inheritance?
Plain @Builder doesn't naturally handle a subclass adding its own fields on top of a parent class's builder — by default, each class's @Builder only knows about that class's own fields. Lombok's @SuperBuilder annotation exists specifically to solve this, generating...
34. What is the difference between @Getter(lazy = true) and a normal @Getter?
A normal @Getter just returns the field's current value directly. @Getter(lazy = true) is meant for a field whose value is expensive to compute: it generates a getter that computes the value only the first time it's called, caches the...
35. Why should you be careful using @EqualsAndHashCode on Hibernate entities with lazy-loaded proxies?
Hibernate sometimes returns a lazy-loading proxy object standing in for the real entity, rather than the actual entity instance itself — and a proxy's runtime class differs from the real entity's class (it's a dynamically generated subclass). A naive equals()...
36. What is @Accessors used for and how does it change fluent-style access?
@Accessors customizes the naming and chaining style of Lombok's generated getters/setters, moving away from strict JavaBeans conventions toward a more fluent, chainable style some codebases prefer. @Accessors (fluent = true, chain = true) @Getter @Setter public class Person { private...
37. How does Lombok handle @ToString with circular references?
Lombok's @ToString doesn't have built-in circular reference detection — if class A has a @ToString -included field pointing to class B, and B has a field pointing back to A, calling toString() on either one recurses infinitely (A's toString calls...
38. What is @FieldDefaults used for?
@FieldDefaults (an experimental annotation) lets you set a default access level and/or final -ness for every field in a class in one line, rather than repeating private final on each field individually. @FieldDefaults (makeFinal = true, level = AccessLevel ....
39. What is @Delegate used for?
@Delegate (experimental) generates wrapper methods on a class that forward calls to a field's methods, implementing the composition-based delegation pattern without hand-writing a pass-through method for every method you want to expose. public class SortedList { @Delegate private final List...
40. Why is Lombok's approach considered a "compiler hack" and what are the implications?
Lombok works by using internal, officially unsupported compiler APIs (from javac 's and Eclipse's own compiler internals) to directly rewrite a class's abstract syntax tree during compilation — something the standard, documented annotation processing API doesn't actually permit (it only...
41. How do you debug generated Lombok code?
Since the generated methods don't appear in your source file, stepping through them in a debugger or inspecting exactly what was generated requires a couple of specific techniques rather than just reading the .java file. # view the actual generated...
42. What issues can Lombok cause with static code analysis tools?
Static analysis tools that operate on source code (rather than compiled bytecode) may not understand Lombok's annotations at all, and so analyze the class as if the generated methods simply don't exist — this can produce both false positives (flagging...
43. What are the risks of using @Data with mutable collections?
A field like private List<String> tags; under @Data gets a plain getter that returns the actual internal list reference, not a defensive copy — meaning external code that calls the getter can mutate the object's internal state directly, bypassing any...
44. How does delombok work and when would you use it?
delombok runs Lombok's own transformation logic but, instead of feeding the result to the compiler, writes out the fully expanded, plain-Java equivalent source code — every getter, setter, constructor, and other generated method spelled out explicitly, with no Lombok annotations...
45. Why might Lombok cause issues in multi-module Maven builds with annotation processor ordering?
Annotation processors run in whatever order the build tool discovers and applies them, and if a project uses multiple processors together (Lombok alongside MapStruct, or a custom code-generating processor), the order they run in can matter — particularly since Lombok's...
46. What is @UtilityClass used for?
@UtilityClass (experimental) converts a class into a static utility class: it makes the class final , generates a private constructor that throws if somehow invoked via reflection, and marks every field and method in the class as static automatically —...
47. What is the difference between Lombok's @Builder and the traditional Gang-of-Four Builder pattern?
The classic Gang-of-Four Builder pattern is typically hand-written as a separate, standalone class (often with its own interface) explicitly responsible for assembling a complex object step by step, sometimes supporting different construction algorithms or representations via a "Director" that or
48. How do you use Lombok with Java records - is Lombok still needed?
Java's built-in record type (since Java 16) already generates a canonical constructor, accessors, equals() , hashCode() , and toString() automatically — covering a large chunk of what @Value or @Data used to provide for immutable classes, without needing Lombok at...