Lombok for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Lombok and why is it needed?



Lombok (Project Lombok) is a library for the Java language that significantly reduces the amount of boilerplate code. In Java, developers often write repetitive constructs: getters, setters, constructors, toString(), equals() and hashCode() methods. Lombok automatically generates this code at compile time using annotations.



The main goal of Lombok is to make code cleaner, shorter, and easier to maintain. Instead of writing dozens of lines of repetitive code, you simply add one annotation, and everything is generated automatically. This is especially useful for model classes (POJO/DTO), where boilerplate code can account for up to 70% of the total volume.



Lombok is not a runtime framework — it works exclusively at compile time. The generated code is embedded into the bytecode, so no Lombok dependencies are required at runtime.



Installing Lombok



Lombok is added as a regular dependency to your project. Since it is a Java library, installation via pip or npm is not applicable. Here are the main installation methods:



Maven

Add to pom.xml:

<dependency>    <groupId>org.projectlombok</groupId>    <artifactId>lombok</artifactId>    <version>1.18.30</version>    <scope>provided</scope></dependency>


Gradle

Add to build.gradle:

dependencies {    compileOnly 'org.projectlombok:lombok:1.18.30'    annotationProcessor 'org.projectlombok:lombok:1.18.30'}


IntelliJ IDEA

In IntelliJ IDEA, you need to install the Lombok plugin (Settings → Plugins → Marketplace → Lombok) and enable annotation processing (Settings → Build, Execution, Deployment → Compiler → Annotation Processors → Enable annotation processing).



Eclipse

Download the Lombok JAR file from the official website and run it by double-clicking — the installer will open, find your IDE automatically, and configure the necessary settings.



Quick Start with Code Example



Let's look at a typical Java class before and after using Lombok.



Without Lombok (traditional approach):

public class User {    private String name;    private int age;    private String email;

public User() {}

public User(String name, int age, String email) { this.name = name; this.age = age; this.email = email; }

public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }

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

@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return age == user.age && Objects.equals(name, user.name) && Objects.equals(email, user.email); }

@Override public int hashCode() { return Objects.hash(name, age, email); }}


With Lombok:

import lombok.*;

@Data@NoArgsConstructor@AllArgsConstructorpublic class User { private String name; private int age; private String email;}


Just 5 lines instead of 50! The code has become concise, readable, and easy to maintain.



Main Lombok Annotations and Methods



Below are the key Lombok annotations. Each of them generates a specific set of

Recommendations