Skip to content

Lombok

Annotations that generate boilerplate — getters, builders, constructors — at compile time.

Developer UtilitiesCLI/UtilsJava

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 IDE

Getting started

The smallest useful thing you can do with it, and what each part means.

Using @Getter and @Setter
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class User {
    private int id;
    private String name;
}
Automatically generates getter and setter methods for `id` and `name` fields.
Using @NoArgsConstructor and @AllArgsConstructor
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
public class User {
    private int id;
    private String name;
}
Generates a no-argument constructor and an all-argument constructor automatically.
Using @ToString and @EqualsAndHashCode
import lombok.ToString;
import lombok.EqualsAndHashCode;

@ToString
@EqualsAndHashCode
public class User {
    private int id;
    private String name;
}
Automatically generates `toString()`, `equals()`, and `hashCode()` methods.

Advanced usage

Where the library earns its place over a simpler alternative.

Using @Builder
import lombok.Builder;

@Builder
public class User {
    private int id;
    private String name;
}

User user = User.builder().id(1).name("Alice").build();
Enables the builder pattern for creating instances of `User`.
Using @Data
import lombok.Data;

@Data
public class User {
    private int id;
    private String name;
}
`@Data` generates getters, setters, `toString()`, `equals()`, `hashCode()`, and required constructors in a single annotation.
Using @Value for immutable objects
import lombok.Value;

@Value
public class User {
    private int id;
    private String name;
}
`@Value` creates immutable classes with final fields, getters, `toString()`, `equals()`, `hashCode()`, and a constructor.
Customizing generated methods
import lombok.Getter;
@Getter(onMethod_ = {@Deprecated})
private int id;
Demonstrates adding custom annotations or behaviors to generated methods using Lombok features.

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.