Skip to main content

Command Palette

Search for a command to run...

Complete Guide to Lombok

Updated
โ€ข5 min read
Complete Guide to Lombok
N

I am a Tech Enthusiast having 13+ years of experience in ๐ˆ๐“ as a ๐‚๐จ๐ง๐ฌ๐ฎ๐ฅ๐ญ๐š๐ง๐ญ, ๐‚๐จ๐ซ๐ฉ๐จ๐ซ๐š๐ญ๐ž ๐“๐ซ๐š๐ข๐ง๐ž๐ซ, ๐Œ๐ž๐ง๐ญ๐จ๐ซ, with 12+ years in training and mentoring in ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐“๐ž๐ฌ๐ญ ๐€๐ฎ๐ญ๐จ๐ฆ๐š๐ญ๐ข๐จ๐ง ๐š๐ง๐ ๐ƒ๐š๐ญ๐š ๐’๐œ๐ข๐ž๐ง๐œ๐ž. I have ๐’•๐’“๐’‚๐’Š๐’๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 10,000+ ๐‘ฐ๐‘ป ๐‘ท๐’“๐’๐’‡๐’†๐’”๐’”๐’Š๐’๐’๐’‚๐’๐’” and ๐’„๐’๐’๐’…๐’–๐’„๐’•๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 500+ ๐’•๐’“๐’‚๐’Š๐’๐’Š๐’๐’ˆ ๐’”๐’†๐’”๐’”๐’Š๐’๐’๐’” in the areas of ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐ƒ๐ž๐ฏ๐ž๐ฅ๐จ๐ฉ๐ฆ๐ž๐ง๐ญ, ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐‚๐ฅ๐จ๐ฎ๐, ๐ƒ๐š๐ญ๐š ๐€๐ง๐š๐ฅ๐ฒ๐ฌ๐ข๐ฌ, ๐ƒ๐š๐ญ๐š ๐•๐ข๐ฌ๐ฎ๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง๐ฌ, ๐€๐ซ๐ญ๐ข๐Ÿ๐ข๐œ๐ข๐š๐ฅ ๐ˆ๐ง๐ญ๐ž๐ฅ๐ฅ๐ข๐ ๐ž๐ง๐œ๐ž ๐š๐ง๐ ๐Œ๐š๐œ๐ก๐ข๐ง๐ž ๐‹๐ž๐š๐ซ๐ง๐ข๐ง๐ . I am interested in ๐ฐ๐ซ๐ข๐ญ๐ข๐ง๐  ๐›๐ฅ๐จ๐ ๐ฌ, ๐ฌ๐ก๐š๐ซ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ค๐ง๐จ๐ฐ๐ฅ๐ž๐๐ ๐ž, ๐ฌ๐จ๐ฅ๐ฏ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐ž๐ฌ, ๐ซ๐ž๐š๐๐ข๐ง๐  ๐š๐ง๐ ๐ฅ๐ž๐š๐ซ๐ง๐ข๐ง๐  new subjects.

Introduction

Lombok is a Java library that helps reduce boilerplate code by automatically generating commonly used methods like constructors, getters, setters, toString, equals, hashCode, and more.

Different annotations

Different annotations in Lombok

  1. Constructor

  2. Getters and Setters

  3. Other Useful Annotations

Constructor Annotations

There are 4 important constructor annotations

  1. @NoArgsConstructor

  2. @AllArgsConstructor

  3. @RequiredArgsConstructor

  4. @Builder

@NoArgsConstructor

Required for frameworks like JPA, Jackson, and Hibernate.

Without Lombok API

public Employee() { }

With Lombok API

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class Employee {
    private String name;
    private int age;
}

@AllArgsConstructor(Constructor with All Fields)

Used to create fully initialized objects.

Without Lombok API

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

With Lombok API

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class Employee {
    private String name;
    private int age;
}

@RequiredArgsConstructor(Constructor for final or @NonNull Fields)

Generates a constructor for only required fields.

Without Lombok API

public Employee(String name) {
    this.name = name;
}

With Lombok API

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class Employee {
    private final String name;
    private int age;
}

@Builder (Fluent Builder Pattern)

Useful for creating objects with optional parameters.

Without Lombok API

package com.module02.builder;

public class Employee {
    private final String name;
    private final int age;

    private Employee(EmployeeBuilder builder) {
        this.name = builder.name;
        this.age = builder.age;
    }

    public static class EmployeeBuilder {
        private String name;
        private int age;

        public EmployeeBuilder name(String name) {
            this.name = name;
            return this;
        }

        public EmployeeBuilder age(int age) {
            this.age = age;
            return this;
        }

        public Employee build() {
            return new Employee(this);
        }
    }

    public static EmployeeBuilder builder() {
        return new EmployeeBuilder();
    }

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

# Main Class
Employee employee = Employee.builder().name("Naveen").age(38).build();
        System.out.println(employee);

With Lombok

import lombok.Builder;

@Builder
public class Employee {
    private String name;
    private int age;
}

Getters and Setters Annotations

There are 3 important constructor annotations

  1. @Getter

  2. @Setter

  3. @Data

@Getter (Generates Getter Methods)

Automatically generates getter methods.

Without Lombok

package com.module02.builder;

public class Employee {
    private final String name;
    private final int age;

    private Employee(EmployeeBuilder builder) {
        this.name = builder.name;
        this.age = builder.age;
    }

    public static class EmployeeBuilder {
        private String name;
        private int age;

        public EmployeeBuilder name(String name) {
            this.name = name;
            return this;
        }

        public EmployeeBuilder age(int age) {
            this.age = age;
            return this;
        }

        public Employee build() {
            return new Employee(this);
        }
    }

    public static EmployeeBuilder builder() {
        return new EmployeeBuilder();
    }

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




# Main Class
Employee employee = Employee.builder().name("Naveen").age(38).build();
        System.out.println(employee);
System.out.println(employee)

With Lombok API

import lombok.Getter;

@Getter
public class Employee {
    private String name;
    private int age;
}

@Setter (Generates Setter Methods)

Without Lombok API

public void setName(String name) {
    this.name = name;
}

public void setAge(int age) {
    this.age = age;
}

With Lombok API


import lombok.Setter;

@Setter
public class Employee {
    private String name;
    private int age;
}

@Getter and @Setter on Specific Fields

If you want getters and setters for only some fields.

Without Lombok API

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

public int getAge() { return age; }

With Lombok API

import lombok.Getter;
import lombok.Setter;

public class Employee {
    @Getter @Setter
    private String name;

    @Getter
    private int age; // Read-only field
}

Restricting Getter and Setter Access

Control access level of getters and setters.

Without Lombok API

protected String getName() { return name; }
private void setAge(int age) { this.age = age; }

With Lombok API

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class Employee {
    @Getter(AccessLevel.PROTECTED)
    private String name;

    @Setter(AccessLevel.PRIVATE)
    private int age;
}

Other Useful Annotations

  1. @ToString

  2. @EqualsAndHashCode

  3. @Value (Immutable class)

  4. @Builder

  5. @Slf4j

@Data (Combines Getters, Setters, and More)

Generates getters, setters, toString(), equals(), and hashCode().

Without Lombok API

public class Employee {
    private String name;
    private int age;

    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; }

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

    @Override
    public boolean equals(Object obj) { ... } 
    @Override
    public int hashCode() { ... }
}

With Lombok API

import lombok.Data;

@Data
public class Employee {
    private String name;
    private int age;
}

@ToString (Generates toString() Method)

Without Lombok API

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

With Lombok API

import lombok.ToString;

@ToString
public class Employee {
    private String name;
    private int age;
}

@EqualsAndHashCode (Generates equals() and hashCode())

Used for object comparison.

Without Lombok API

@Override
public boolean equals(Object o) { ... }

@Override
public int hashCode() { ... }

With Lombok API

import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class Employee {
    private String name;
    private int age;
}

@Value (Immutable Class)

Creates immutable objects (like DTOs).

With Lombok API

import lombok.Value;

@Value
public class Employee {
    String name;
    int age;
}
  • All fields are private final.

  • No setters are generated.

  • Generates getters, toString(), equals(), and hashCode().

@Slf4j (Logger)

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EmployeeService {
    public void processEmployee() {
        log.info("Processing employee...");
    }
}
A

There is a typo in getter part(copied code from builder pattern) and in builder pattern changed places of "with lombok" and "without lombok"

N
Naveen Pn1y ago

Thank you vm , its resolved

More from this blog

Naveen P.N's Tech Blog

94 posts