What is Lombok?

Lombok is a compile-time code generator for Java that uses annotations to remove boilerplate (getters, setters, constructors, builders, equals/hashCode, toString, loggers, etc.). It works via annotation processing: your IDE/build runs Lombok, which generates the missing code into the compiled classes.

Why use it?

  • Fewer lines of code, more readable POJOs/DTOs
  • Consistent, generated equals/hashCode/toString
  • Easy builders and immutables
  • Built-in logging fields

Core annotations (most used)

  • @Getter, @Setter – generate accessors (field/class level)
  • @ToString, @EqualsAndHashCode
  • @NoArgsConstructor, @AllArgsConstructor, @RequiredArgsConstructor
  • @Data – shortcut for: getter/setter + equals/hashCode + toString + required constructor
  • @Builder (+ @SuperBuilder, @Singular)
  • @Value – immutable class (all fields private final, getters, proper equals/hashCode)
  • @Slf4j (and friends) – adds a logger field
  • @NonNull – null-checks
  • @With – copy-with semantics
  • @SneakyThrows – rethrow checked exceptions without declaring (use carefully)

Quick before/after

Without Lombok

public class User {
  private String id;
  private String name;

  public User() {}
  public User(String id, String name) { this.id = id; this.name = name; }

  public String getId() { return id; }
  public void setId(String id) { this.id = id; }
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }

  @Override public String toString() { return "User{id='" + id + "', name='" + name + "'}"; }
}

With Lombok

import lombok.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
  private String id;
  private String name;
}

Builder & immutable example

import lombok.*;

@Value
@Builder(toBuilder = true)
public class Address {
  String line1;
  String city;
  String pin;
}

Logging

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Service {
  public void work() {
    log.info("Hello from Lombok logger");
  }
}

Setup

Gradle

dependencies {
  compileOnly 'org.projectlombok:lombok:1.18.34'
  annotationProcessor 'org.projectlombok:lombok:1.18.34'
  testCompileOnly 'org.projectlombok:lombok:1.18.34'
  testAnnotationProcessor 'org.projectlombok:lombok:1.18.34'
}

Maven

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.34</version>
  <scope>provided</scope> <!-- compile-time only -->
</dependency>

IDE

  • IntelliJ: enable Annotation Processing; install Lombok plugin for better hints.
  • Eclipse: run the Lombok JAR once to hook into Eclipse (or use newer Eclipse with built-in support).
  • VS Code: install Lombok extension.

Gotchas / best practices

  • @Data on entities: can cause issues with JPA and collections in equals/hashCode. Prefer:
    • @Getter @Setter @ToString @EqualsAndHashCode(onlyExplicitlyIncluded = true) public class Person { @EqualsAndHashCode.Include private Long id; // ... }
  • Immutability: use @Value (or Java records in 16+); don’t mix mutable fields.
  • @SneakyThrows: handy, but hides checked exceptionsβ€”use sparingly.
  • Binary size & debugging: generated methods exist in bytecode, so stack traces are fine; source may look shorter than decompiled classβ€”use β€œdelombok” if you need plain source for tools.
  • Alternative tools: Java records, Immutables, AutoValueβ€”choose per team standards.
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.