What is the need of Lombok?
Java is a very popular language but it has few drawbacks. One of the most popular drawback is that we still need to write the boilerplate codes like getters, setters, toString method in Java whereas Kotlin and Scala, which are also JVM based doesn’t need so and hence, this is the reason of their increased popularity in the community. This is where Lombok comes in picture and overcomes this drawback of Java.
What is Lombok?
Project Lombok is a java library tool which 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.
- Without Lombok: A java model class with four private fields and their getters, setters, no args constructor, parameterized construct and toString method.
publicclassEmployee {privateInteger employeeId;privateString name;privateString company;privateString emailId;publicEmployee(){}publicEmployee(Integer employeeId, String name,String company, String emailId){super();this.employeeId = employeeId;this.name = name;this.company = company;this.emailId = emailId;}publicInteger getEmployeeId(){returnemployeeId;}publicvoidsetEmployeeId(Integer employeeId){this.employeeId = employeeId;}publicString getName(){returnname;}publicvoidsetName(String name){this.name = name;}publicString getCompany(){returncompany;}publicvoidsetCompany(String company){this.company = company;}publicString getEmailId(){returnemailId;}publicvoidsetEmailId(String emailId){this.emailId = emailId;}@OverridepublicString toString(){return"Employee ["+"employeeId="+ employeeId+", name="+ name +", "+" company="+ company+", emailId="+ emailId+"]";}}chevron_rightfilter_none- 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.
importLombok.AllArgsConstructor;importLombok.Getter;importLombok.NoArgsConstructor;importLombok.Setter;importLombok.ToString;@Getter@Setter@NoArgsConstructor@AllArgsConstructor@ToStringpublicclassEmployee {privateInteger employeeId;privateString name;privateString company;privateString emailId;}chevron_rightfilter_noneThe 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.
How to configure project Lombok to eclipse?
- Download the Lombok jar file from this official link.
- Double click on the downloaded jar file to execute it. A GUI will appear to specificy the IDE in which you want to plugin it.
- Click on specify location and select the IDE.
- Click on install to complete the installation and then quit the installer.
- 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.- @Getter and @Setter: These annotations provides 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”.
importLombok.Getter;importLombok.Setter;@Getter@SetterpublicclassEmployee {privateInteger employeeId;privateString name;privateString company;privateString emailId;}chevron_rightfilter_none - @NoArgsConstructor: This annotation is used to generate a constructor with no arguments. It has 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.
importLombok.NoArgsConstructor;@NoArgsConstructorpublicclassEmployee {privateInteger employeeId;privateString name;privateString company;privateString emailId;}chevron_rightfilter_none - @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.
importLombok.AllArgsConstructor;@AllArgsConstructorpublicclassEmployee {privateInteger employeeId;privateString name;privateString company;privateString emailId;}chevron_rightfilter_none - @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.
importLombok.ToString;@ToStringpublicclassEmployee {privateInteger employeeId;privateString name;privateString company;privateString emailId;}chevron_rightfilter_none - @EqualsAndHashCode: This annotation is used to override the equals() and hashCode() methods and provides a default impluentation for thu. The default impluentation uses all the non-static fields and we can modify it and can exclude some fileds using the annotation @EqualsAndHashCode.Exclude.
importLombok.EqualsAndHashCode;@EqualsAndHashCodepublicclassEmployee {privateInteger employeeId;privateString name;privateString company;privateString emailId;}chevron_rightfilter_none - @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 impluentation for toString(), equals() and hashCode() using all the fields of the class and a constructor that initializes all the fields of the class.
importLombok.Data;@DatapublicclassEmployee {privateInteger employeeId;privateString name;privateString company;privateString emailId;}chevron_rightfilter_none
References:
- https://www.baeldung.com/intro-to-project-Lombok
- https://dzone.com/articles/project-Lombok-boilerplate-code-reducer
- https://projectLombok.org/
Recommended Posts:
- Project Idea | (Project Approval System)
- Apache POI | Getting Started
- Getting Started with Cross-Platform Mobile Application using Flutter
- Project Idea | Know More
- Project Idea | Get Me Through
- Project Idea | God's Eye
- Project Idea | Jal Sanrakshan
- Project Idea | APTrack
- Project Idea | SmartStreet
- Project Idea | (CSE Webnode)
- Project Idea | Get Your Logo
- Project Idea | LinkBook
- Project Idea | Next Stop
- Project Idea | safeT
- Project Idea| CamCaliber
- Project Idea | Social - Cop
- Project Idea | Re-Medi
- Project Idea | Knowledge Value
- Project Idea | Express
- Project Idea | Payto
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
- 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.


