Java Lombok sounds pretty complex. But, in the real world, it is developer-friendly and actually helps developers to avoid writing boilerplate.
Lombok is a Java library which helps us to minimize boilerplate code.
There are multiple places in Java where we write boilerplate code such as setters, getters, toString, constructor etc.
Lombok provides us with a number of annotations to avoid writing boilerplate code and, at the same time, helps us to keep Java class neat and clean.
Java compiler helps us to generate or include boilerplate code at compile time based on annotations we include. Lombok will not generate boilerplate code at run time if we have already written code manually in class. e.g. @ToString Lombok annotation will not create boilerplate code if toString() method is already written in java class.
Include below maven dependency in pom.xml to consume Lombok annotations.
Below is an example of Student.java class having Lombok annotations such as @ToString, @Getter, @Setter, @NoArgsConstructor, @AllArgsConstructor.
@Getter and @Setter annotation helps us to generate below boilerplate code.
@ToString allow us to create toString() including all fields.
@NoArgsConstructor allow us to create empty constructor.
@AllArgsConstructor allow us to create parameterized constructor.
Below @Data Lombok annotation automatically generates @Getter, @Setter, @ToString, @RequiredArgsConstrutor and @EqualsAndHashcode annotations.
@Data equivalent to @Getter @Setter @ToString @RequiredArgsConstrutor @EqualsAndHashcode
Follow below steps to configure Lombok in an IDE or Eclipse. Below example is to install Lombok in Eclipse.
3. Click on install/Update button.
Above installation will just include below entry inside eclipse.ini file:
-javaagent:C:\Users\imrankhan5\eclipse\jee-2019–122\eclipse\lombok.jar
4. Open Eclipse and in top menu click on help → About Eclipse IDE in top menu to identify if Lombok setup is successfully done.
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved