What is Project Lombok?

minelaydin
Analytics Vidhya
Published in
3 min readSep 21, 2021

Hello all, in this post I want to talk about Lombok Project.

In our Java projects there are always some templates that we have to write (getter, setter, constructor, toString, equals, hashCode etc.). But these codes contain a lot of space in our Java projects and reduce readability. Project Lombok is helping us out here. It is a library with saving unnecessary number of lines of code, increasing readability and ensuring that we write cleaner code.

But how? Let’s get started..

First we need to add lombok dependency to our pom.xml:

pom.xml

Lombok Annotations

@Getter and @Setter

The annotation we will write to create the Getter and the Setter function of the variable. If we do not specify the access level, the generated getter/setter method will be public in default.

Getter/Setter

@NonNull

This annotation controls if the object is null. If null is set, it throws NullPointerException.

NonNull

@EqualsAndHashCode

The annotation creates the Equal and HashCode methods of the bean variables. By default, it’ll use all non-static, non-transient fields, but you can modify which fields are used by marking type members with @EqualsAndHashCode.Include or @EqualsAndHashCode.Exclude.

EqualsAndHashCode

@ToString

The ToString annotation creates the class-level ToString method. You can pass as a parameter which fields should contain, which fields should not use.

ToString

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

These parameters are used to create the constructor of the class. @NoArgsContructor generates a constructor with no parameters. @RequiredArgsConstructor generates a constructor only with nonNull parameters. @AllArgsConstructor generates a constructer with all parameters.

@Data

This annotation performs all the functions of all annotations. It includes toString, hashCode, getter/setter and RequiredArgsConstructor. With @Data annotation, we specify only the objects that we need. Look at the picture below, how short it is right?

Data

If we would not use @Data, we would have to write the getter/setter and other functions which look very long in a class. Now we have a more orderly view.

Thank you for reading :)

https://projectlombok.org/

--

--