Introduction to Project Lombok in Java and How to get started?

20 Jun, 2022
Java is a very popular language, but it has a few drawbacks. One of the most popular drawbacks is that we still need to write the boilerplate codes like getters, setters, and toString() method in Java whereas Kotlin and Scala, which are also JVM based don’t need so, and hence, this is the reason for their increased popularity in the community. This is where Lombok comes into the picture and overcomes this drawback of Java. Project Lombok is a java library tool that is used to minimize/remove the boilerplate code and save the precious time of developers during development by just using some annotations. In addition to it, it also increases the readability of the source code and saves space. But you might be thinking that nowadays, everyone uses IDEs which provides an option for generating these boilerplate codes, then what is the use of Lombok. Whenever we use IDEs to generate these boilerplate codes, we just save ourselves from writing all these codes but it is actually present in our source code and increases the LOC (lines of code), and reduces maintainability and readability. On the other hand, Lombok adds all these boilerplate codes at the compile time in the “.class” file and not in our source code. Let us compare our source code with and without using Lombok.
1. Without Lombok: A java model class with four private fields and their getters, setters, no-args constructor, parameterized construct, and toString method.
Java
public class Employee {     private Integer employeeId;    private String name;    private String company;    private String emailId;     public Employee() {}     public Employee(Integer employeeId, String name,                    String company, String emailId)    {        super();        this.employeeId = employeeId;        this.name = name;        this.company = company;        this.emailId = emailId;    }     public Integer getEmployeeId() { return employeeId; }     public void setEmployeeId(Integer employeeId)    {        this.employeeId = employeeId;    }     public String getName() { return name; }     public void setName(String name) { this.name = name; }     public String getCompany() { return company; }     public void setCompany(String company)    {        this.company = company;    }     public String getEmailId() { return emailId; }     public void setEmailId(String emailId)    {        this.emailId = emailId;    }     @Override public String toString()    {        return "Employee ["            + "employeeId=" + employeeId + ", name=" + name            + ", "            + " company=" + company + ", emailId=" + emailId            + "]";    }}
2. With Lombok: A java model class with four private fields and their getters, setters, no-args constructor, parameterized construct, and toString method using Lombok annotations. 
Java
import lombok.AllArgsConstructor;import lombok.Getter;import lombok.NoArgsConstructor;import lombok.Setter;import lombok.ToString; @NoArgsConstructor@AllArgsConstructor@ToStringpublic class Employee {    private @Getter @Setter Integer employeeId;    private @Getter @Setter String name;    private @Getter @Setter String company;    private @Getter @Setter String emailId;}
The difference is clearly visible in the codes. The original source code contains around 60 lines of code which has been reduced by Lombok to around 20 lines of code only. It not only reduces the lines of code but also increases the readability and maintainability of source code.
1. Download the Lombok jar file from this official link.
2. Double click on the downloaded jar file to execute it. A GUI will appear to specify the IDE in which you want to plugin it.  
3. Click on specify a location and select the IDE.
4. Click on install to complete the installation and then quit the installer.
5. To verify the correct installation, go to the Eclipse IDE, select help, and then about the eclipse. You will see project Lombok installed there. 
Lombok annotations: Lombok provides a set of annotations to make our coding life easier. Let us look at the few most frequently used annotations of Lombok. 
1. @Getter and @Setter: These annotations provide the getter and setter methods for a field. These annotations can be used at both the levels, field as well as class. @Getter annotation generates a getter method with access type as public which simply returns the field and with name getName() if the field name is “Name”. @Setter annotation generates a setter method with access type as public which returns void and takes a single parameter to assign the value to the field. The default setter will have the name setName() if the field name is “Name”.  
Java
import lombok.Getter;import lombok.Setter; public class Employee {    private @Getter @Setter Integer employeeId;    private @Getter @Setter String name;    private @Getter @Setter String company;    private @Getter @Setter String emailId;}
2. @NoArgsConstructor: This annotation is used to generate a constructor with no arguments. It has an empty body and does nothing. It is generally used in combination with some other parameterized constructor in use. It is required when you want to generate an object of the class by passing no arguments in the constructor.  
Java
import lombok.NoArgsConstructor; @NoArgsConstructorpublic class Employee {    private Integer employeeId;    private String name;    private String company;    private String emailId;}
3. @AllArgsConstructor: This annotation is used to generate a parameterized constructor which accepts a single parameter for each field and initializes them using it. It is required when you want to generate an object of the class by passing the initial values of the fields in the constructor.  
Java
import lombok.AllArgsConstructor; @AllArgsConstructorpublic class Employee {    private Integer employeeId;    private String name;    private String company;    private String emailId;}
4. @ToString: This annotation is used to override the toString() method and generate a default implementation for it. The default implementation prints the class name and the fields in order, separated by commas. You can also skip some fields that you don’t want to print by annotating them with @ToString.Exclude.  
Java
import lombok.ToString; @ToStringpublic class Employee {    private Integer employeeId;    private String name;    private String company;    private String emailId;}
5. @EqualsAndHashCode: This annotation is used to override the equals() and hashCode() methods and provides a default implementation for this. The default implementation uses all the non-static fields, and we can modify it and can exclude some fields using the annotation @EqualsAndHashCode.Exclude.  
Java
import lombok.EqualsAndHashCode; @EqualsAndHashCodepublic class Employee {    private Integer employeeId;    private String name;    private String company;    private String emailId;}
6. @Data: This annotation is a shortcut annotation and bundles @ToString, @Getter, @Setter, @EqualsAndHashCode and @RequiredArgsConstructor annotations into a single annotation. This annotation provides all the normally used boilerplate code in the model classes of java like getters for all the fields, setter for all the non-final fields, a default implementation for toString(), equals() and hashCode() using all the fields of the class and a constructor that initializes all the fields of the class. 
Java
import lombok.Data; @Datapublic class Employee {    private Integer employeeId;    private String name;    private String company;    private String emailId;}
7. @Builder: This annotation can be used remove boilerplate code involved in setting properties for an object. This is useful for POJO’s which have many fields.With this annotation setting properties for an object can be done in a single statement of code thereby making it readable and clean.
Java
import lombok.Builder;  @Builderpublic class Employee {    private Integer employeeId;    private String name;    private String company;    private String emailId;}
Example: Using @Builder 
Java
import java.io.*; class LombokTest {    public static void main (String[] args) {        Employee employee = Employee.builder().employeeId(21).name("GFG").company("GeeksForGeeks")          .emailId("gfg@geeks.com").build();    }}
chbs17cs
Anshul_Aggarwal
venkatanirudh36
vinamrayadav
java-advanced
Articles
Java Programs
Project
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
Time complexities of different data structures
Difference Between Object And Class
SQL | DROP, TRUNCATE
Implementation of LinkedList in Javascript
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class