Akshay Ingole
Globant
Published in
4 min readMay 4, 2022

--

Getting Started with Lombok

Java is the most popular object-oriented programming language but it has some drawbacks. The major drawback is to write lots of boilerplate code. To overcome this drawback, project Lombok comes into existence. It is a tool that spices up our Java application.

What is project Lombok?

  • The project Lombok is a popular and widely used Java library that is used to minimize or remove the boilerplate code. It saves time and effort. Just by using the annotations.
  • we can save space and readability of the source code. It is automatically plugging into IDEs and build tools to spice up our Java application.

Why project Lombok?

  • Suppose, we are developing a Java application for which a POJO file is required that has several private fields. For these fields, we have to generate getters and setters accessor methods to provide access. Generating getters and setters for each field increases the line of code.
  • Moreover, adding a constructor and a toString() technique will cause much more lines of code and mess. What about when we are utilizing Java objects that need to be closed after use, so we need to code a finally-block or use try-with-resources to ensure that the object closing occurs. Adding a finally-block boilerplate to close objects can add a significant amount of clutter to the code.
  • Hence, we deal with lots of boilerplate code.
  • To overcome the same problem, the project Lombok comes into existence.

Features of Lombok Project :
1. It reduces the boilerplate code.
2. It replaces boilerplate code with easy-to-use annotations.
3. It makes code easier to read and less error-prone.
4. By using Lombok the developers becomes more productive.
5. It works well with all popular IDEs.
6. Provide annotation for checking null values.
7. Easy cleanup
8. Locking safely
9. Effortless logging

Purpose to Use Lombok Project:
Lombok is used to reduce boilerplate code automatically by using Lombok annotations.

  • The @Data Lombok produces getters and setters for every one of the non-static class fields and a class constructor.
  • The @AllArgsConstructor annotation generates a constructor with all fields that are declared.
  • The @NoArgsConstructor annotation simply generates the constructor without any argument.
  • The library @NonNull annotation that can be used to generate a null check on a setter field.
  • The @Getter and @Setter annotation generating getters and setters can be laborious work if there are several private fields in the POJO file.

Example :

  1. with All annotations
image: Declaration of Lombok annotation
  • Using above Lombok annotations will generates the boiler plate code (getters, setters, toString(), constructor) automatically, which happens at compile time, so developer no need to write code manually for the setters, getters, overriding toString(), equals(), hashcode(), generating with arguments and default constructors etc.

2.With @Data annotations and there internal implementation.

@Data annotation example

By simply adding the @data annotation you get all this for free:

Setup Lombok in Your Ides :
https://projectlombok.org/setup/eclipse
https://projectlombok.org/setup/intellij

Using Lombok in Java projects :
Using Gradle :
In order to add lombok to the classpath of a Java Project with Gradle the following dependency has to be added:

dependencies {
compileOnly('org.projectlombok:lombok:1.16.20')
}

Using Maven:
In order to add lombok to the classpath of a Java Project with Apache Maven the following dependency has to be added:

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>

Doing it manually
The lombok library can also be downloaded directly from here: https://projectlombok.org/download.html and be added to the classpath.

Conclusion :

  • There are still some other features we haven’t presented in this article. We can take a deeper dive into the feature overview for more details and use cases.
  • Furthermore, most functions we’ve shown have a number of customization options that we may find handy. The available built-in configuration system could also help us with that.
  • Now that we can give Lombok a chance to get into our Java development tool set, we can boost our productivity.

Referances:
https://projectlombok.org/features/all
https://projectlombok.org/setup/eclipse
https://projectlombok.org/download

--

--