Getter, Setter and Constructors with Project Lombok

In this tutorial, I will show you how we can use Project Lombok to generate Getter, Setter methods or the constructors in a Java object automatically.
OK, first of all, we need to create an example Maven project.

Getter, Setter and Constructors with Project Lombok

with Project Lombok dependency as below:

Remember that, we need to install Project Lombok plugin into our IDE, so that the IDE can understand and no compile errors. See construction for IntelliJ IDEA at 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, Setter methods in Student class and just for Project Lombok generate them by using @Getter, @Setter annotation:

and add a default constructor, a constructor with all properties into Student class by using @NoArgsConstructor and @AllArgsConstructor annotation as below:

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

We can declare Getter, Setter only for some properties by using @Getter, @Setter annotation for that properties. For example, I only need Getter, Setter method for property code in Student class, I will declare as below:

Now, you can only Getter, Setter for property code for Student object like below:

Getter, Setter and Constructors with Project Lombok

Add Comment