Introduction to Project Lombok

Project Lombok is a Java library that uses annotations to generate templated code during compilation, such as Getters, Setters, and constructors. Project Lombok annotations like @Getter, @Setter, @NoArgsConstructor, @AllArgsConstructor, @Data, etc., allow us to minimize the need to manually write repetitive code. In this tutorial, I will show you how to use Project Lombok to automatically generate Setter, Getter methods and constructors in a Java class!

First of all, I will create an example Maven project.

Getter, Setter and Constructors with Project Lombok

with Project Lombok dependency as below:

You also need to configure the annotation processor in the Maven Compiler Plugin as follows:

so when you compile the code with Maven, the code related to Lombok will be processed.

For the IDE you’re working with, you also need to install the Project Lombok plugin so that the IDE can understand it and avoid compilation errors. You can find instructions on installing the Project Lombok plugin in IntelliJ IDE here.

Now, for example, I have a Student class with some information like name, code, dateOfBirth. Without using Project Lombok library, I must declare Student class as below:

With Project Lombok, I can remove Getter and Setter methods in Student class and just for Project Lombok to generate them by using @Getter, @Setter annotations:

For constructors, you can add a constructor with no parameters and a constructor with all the information of the Student class by using the annotations @NoArgsConstructor and @AllArgsConstructor as follows:

If your class defines some required fields (final fields or fields defined with Lombok’s @NonNull annotation), you can also use the @RequiredArgsConstructor annotation to generate constructors for these required fields, as follows:

When Maven or your IDE compiles the Student class, you will see that the content of the Student class includes Getters, Setters, and a constructor as follows:

All the Getters, Setters, and constructors have been fully generated.

Now, you can use our Student class with Getter, Setter, Constructors which we already declared with Project Lombok. For example:

Result:

Getter, Setter and Constructors with Project Lombok

If the @Getter and @Setter annotations are declared at the class level, Project Lombok will generate Getter and Setter for all non-static fields of that class. You can also declare Getter and Setter for only certain fields of a Java class using the @Getter and @Setter annotations. For example, if I only want to declare Getter and Setter for the code field of the Student class, I would declare it as follows:

Now, you can only use Getter and Setter for the code field of the Student class:

You can also define access modifiers for these Getter and Setter methods and constructors using the AccessLevel class, for example:

At this point, Maven or the IDE will generate the Student class during compile time as follows:

As you can see, the constructor with no parameters is generated with the access modifier private. The Setter method for the code field is generated with the access modifier private. And the Setter method for the dateOfBirth field has been generated with the access modifier protected.

Add Comment