In this Project Lombok tutorial, you will learn everything about the Lombok configuration, Lombok Annotations & Use Cases with examples. Lombok annotation will help a lot to you application development faster and clean code.
Why Project Lombok?
Java is broadly used object-oriented programming language. Java has major drawbacks of writing boilerplate code. To overcome this major drawback, save time and efforts of developers, Project Lombok comes into existence. When using any IDE to generate boilerplate code (getters/getters/constructor/toString()/hashCode() methods) or write is manually, it exist in source code that increase the lines of code, and reduce readability and maintainability of class. While using the Project Lombok annotations will reduce or remove all these boilerplate code.
What is Project Lombok?
As per Project Lombok
“Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.”
Project Lombok provides annotation-based Java library that allows you to minimize or remove boilerplate code of your application which saves time and effort of developers. Lombok offers various annotations for replacing Java code that is well known for being boilerplate, repetitive, or tedious to write. These annotation makes you Java classes clean for readability and maintainability.
Example : Lombok annotation @Data on class level can take care of all you class getter/setter, toString() method, constructors (default, parametrize), equals() method and hashCode() method. so specifically no need to write or generate this code. Now from above example you can understand how useful is Project Lombok for save time, effort and maintainability of Java application.
Features of Project Lombok

How Project Lombok works?
Lombok Project dependency will add to your application which will generate all the required code on compile based on annotations specify in class. So your class source code will be clean however your generated compiled class will have all the required methods as per annotations on class and field levels.

How to configure Project Lombok?
Build Tools Lombok Configuration
IDE Lombok Configuration
- Eclipse
- MyEclipse
- Intellij IDEA
- STS (Spring Tool Suite)
- NetBeans
- Jboss Developer Studio
- Visual Studio Code
Project Lombok Packages
The Project Lombok includes the following packages that are having many Lombok annotations and Java API for different purposes.
- lombok
- experimental
- extern.apachecommons
- extern.flogger
- extern.java
- extern.jbosslog
- extern.log4j
- extern.slf4j
Project Lombok Classes
Class | Description |
ConfigurationKeys | A container class containing all Lombok configuration keys that do not belong to a specific annotation. |
Project Lombok Enums
Enum | Description |
AccessLevel | Represents an AccessLevel. |
Lombok Annotations & Use Cases
These are very useful Project Lombok annotations which can be use in any Java based applications. Click on Lombok annotations & Use cases to get example.

Annotation | Description |
@AllArgsConstructor | Generates an all-args constructor. |
@Builder | The @Builder is class level annotation. @Builder creates a so-called ‘builder’ aspect to the class or the class that contains a member which is annotated with @Builder. |
@Builder.Default | The field annotated with @Default must have an initializing expression; that expression is taken as the default to be used if not explicitly set during building. |
@Builder.ObtainVia | Put on a field (in case of @Builder on a type) or a parameter (for @Builder on a constructor or static method) to indicate how Lombok should obtain a value for this field or parameter given an instance; this is only relevant if toBuilder is true. |
@Cleanup | Ensures the variable declaration that you annotate will be cleaned up by calling its close method, regardless of what happens. |
@CustomLog | Causes Lombok to generate a logger field based on a custom logger implementation. |
@Data | Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check all non-transient fields. |
@EqualsAndHashCode | Generates implementations for the equals and hashCode methods inherited by all objects, based on relevant fields. |
@EqualsAndHashCode.Exclude | If present, do not include this field in the generated equals and hashCode methods. |
@EqualsAndHashCode.Include | Configure the behavior of how this member is treated in the equals and hashCode implementation; if on a method, include the method’s return value as part of calculating hashCode/equality. |
@Generated | Lombok will eventually automatically add this annotation to all generated constructors, methods, fields, and types. |
@Getter | Put on any field to make Lombok build a standard getter. |
@NoArgsConstructor | Generates a no-args constructor. |
@NonNull | If put on a parameter, Lombok will insert a null-check at the start of the method /constructor’s body, throwing a NullPointerException with the parameter’s name as a message. |
@RequiredArgsConstructor | Generates a constructor with required arguments. |
@Setter | Put on any field to make Lombok build a standard setter. |
@Singular | The singular annotation is used together with @Builder to create single element ‘add’ methods in the builder for collections. |
@SneakyThrows | @SneakyThrow will avoid javac’s insistence that you either catch or throw onward any checked exceptions that statements in your method body declare they generate. |
@Synchronized | Almost exactly like putting the ‘synchronized’ keyword on a method, except will synchronize on a private internal Object, so that other code not under your control doesn’t meddle with your thread management by locking on your own instance. |
@ToString | Generates an implementation for the toString method inherited by all objects, consisting of printing the values of relevant fields. |
@ToString.Exclude | If present, do not include this field in the generated toString. |
@ToString.Include | Configure the behavior of how this member is rendered in the toString; if on a method, include the method’s return value in the output. |
@val | Use val as the type of any local variable declaration (even in a for-each statement), and the type will be inferred from the initializing expression. |
@Value | Generates a lot of code that fits with a class that is a representation of an immutable entity. |
@var | Use var as the type of any local variable declaration (even in a for statement), and the type will be inferred from the initializing expression (any further assignments to the variable are not involved in this type inference). |
@With | Put on any field to make Lombok build a ‘with’ – a withX method that produces a clone of this object (except for 1 field which gets a new value). |
References
Conclusions
In this Project Lombok Tutorial you have learned about everything about the Lombok annotations & uses cases with examples. Also learn about the dependencies configurations in applications and plugins configurations in different IDE.
Related Posts
Your Feedback Motivate Us
If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.
Happy Learning !!!
You must log in to post a comment.