Lombok
Annotations that generate boilerplate — getters, builders, constructors — at compile time.
What it is
Lombok is a Java library that reduces boilerplate code by generating commonly used methods like getters, setters, constructors, `toString`, `equals`, and `hashCode` using annotations.
Lombok provides annotations like `@Getter`, `@Setter`, `@NoArgsConstructor`, `@AllArgsConstructor`, `@Builder`, `@Data`, and more. These annotations automatically generate code during compilation, reducing boilerplate while keeping Java code clean.
- Licence
- MIT
- Watch for
- `@Data` on JPA entities generates equals/hashCode that break with lazy loading
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Existing codebases on older Java versions where records are not available
- You want builders and `@Slf4j` without writing them
Look elsewhere when
- Java 16+ — `record` covers the common case natively with no build-time magic
- You dislike tooling that requires an IDE plugin to render your code correctly
Installation
Add org.projectlombok:lombok dependency in pom.xml and enable annotation processing in IDEGetting started
The smallest useful thing you can do with it, and what each part means.
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
private int id;
private String name;
}import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class User {
private int id;
private String name;
}import lombok.ToString;
import lombok.EqualsAndHashCode;
@ToString
@EqualsAndHashCode
public class User {
private int id;
private String name;
}Advanced usage
Where the library earns its place over a simpler alternative.
import lombok.Builder;
@Builder
public class User {
private int id;
private String name;
}
User user = User.builder().id(1).name("Alice").build();import lombok.Data;
@Data
public class User {
private int id;
private String name;
}import lombok.Value;
@Value
public class User {
private int id;
private String name;
}import lombok.Getter;
@Getter(onMethod_ = {@Deprecated})
private int id;Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Compilation errors related to Lombok
- Ensure annotation processing is enabled in the IDE and Maven/Gradle plugins are configured correctly.
- IDE does not recognize generated methods
- Rebuild the project or refresh IDE caches; ensure Lombok plugin is installed.
Best practices
- Use Lombok annotations to reduce boilerplate but avoid overusing `@Data` on entities with sensitive fields.
- Enable annotation processing in your IDE for proper code generation.
- Combine `@Builder` with `@AllArgsConstructor` for flexible object creation.
- Prefer immutable classes (`@Value`) where possible for safer code.
- Use `@Getter` and `@Setter` selectively for better control over field access.
Alternatives
Comparable options, and the reason you would pick one over the other.
Java records
Native, no annotation processor, no plugin — prefer where they fit
Background
Why it exists, and what it was reacting to.
Lombok was created to simplify Java development by automating repetitive coding tasks. It uses annotation processing to generate code at compile-time, keeping the source code clean and readable while maintaining full type safety.
