top of page

Project Lombok help

What is Project Lombok?

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.


Boilerplate code:

Boilerplate codes are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered verbose, the programmer must write a lot of boilerplate code to accomplish only minor functionality


Why Project Lombok?

you might be thinking that nowadays, everyone uses IDEs which provides an option for generating these boilerplate codes, then why do we use 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.

Java code with and without Lombok:

Without Lombok: A java model class with two private fields and their getters, setters, parameterized construct, and toString method.


public class Employee {
 
    private Integer employeeId;
    private String name;

    public Employee(Integer employeeId, String name)
    {
        this.employeeId = employeeId;
        this.name = name;
    }
 
    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; 
    }
    
    @Override public String toString()
    {
        return "Employee [" + "employeeId=" + employeeId + ", name=" + name + "]";
    }
}

With Lombok: A java model class with two private fields and their getters, setters, parameterized construct, and toString method using Lombok annotations.


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
 
@AllArgsConstructor
@ToString
public class Employee {
    private @Getter @Setter Integer employeeId;
    private @Getter @Setter String name;
}

you can see the difference between Lombok code and without Lombok code. The without Lombok 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.


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”.

  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.

  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.

  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.

If you want any help in java assignment then please send help request atcontact@codersarts.com or fill the form or Chat with website assistant

bottom of page