5 octubre, 2024

Lombok: forget getters and setters in your Java classes | Bootcamps

Today’s post is special for experienced Androiders, and is in charge of Diego Frenicheexpert multiplatform developer and Instructor of the Intermediate Android Programming Course at .

Get to know the Lombok Project and discover how it will help you optimize lines of code in your development.

We start with an example

Suppose we want to work with people data. We can define a class in the following way:

To this class, known as a POJO (old-fashioned Java object), we can add public getters and setters either by hand or with the help of our favorite IDE to convert it into a JavaBean. The problem is that those getters and setters often don’t contribute much (other times they do, be careful). And we end up with filler code like:

And if, current IDEs do this job for us. But that code will always be there. When reading it. It would not be possible dispense with this «padding code» but have all the advantages ofgetters and setters? What if we could have other advantages? For example, implementing hashcode or equals in our class. Well, the Lombok Project allows us to do all this.

For it to work, we need to add the Lombok library to our project, either by downloading and adding the corresponding jar or using Gradle. If you use Eclipse, it is best to download lombok.jar and run it (it will modify your Eclipse so that Lombok works)

Getters and Setters

The @Getter and @Setter annotations do exactly what their name suggests: add for us a getter/setter for each property.

In this example, the zipCode property has no getter defined. So we can write something like:

Other pearls

Lombok also has annotations to prevent a property from being null, like @NonNull or @ToString to generate this method with the fields that we tell it. But the best one may be @EqualsAndHashCode, which avoids us having to write implementations of these methods, something necessary if we want to serialize our class, or use it in a data structure that handles object hashes.

So, if we write:

We can use toString by

System.out.println(diego.toString());

which returns (age is not included):

And if we try to set the name to null, since it is Non-Nullable we will find with one exception:

@Data: I want it all

@Data gives us all at once:

@EqualsAndHashCode @Getter and @Setter for all properties @ToString A public constructor with all properties not marked @NonNull.

Bonuses

If you want to have implemented a Builder pattern (with a static factory method) To build our objects in a simple way, use the @Builder annotation. Together with @Data, it allows you to write a class like this:

And then be able to use it like this:

So remember: use Lombok to reduce the padding code in your projects.

Try this lightweight bookstore and tell us what you think. Did you find it useful?

Master Android and learn about all the possibilities it offers in the Startup Engineering Bootcamp.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *