If you are a starter developer, this post is for you.

Who has never got fed up with all the generic purpose getters and setters we need to write? Even with the functionalities IDEs provide to automate it, we still have that amount of redundant code written in the classes.

Many years back I found an awesome project for productivity, that uses annotations to create getters and setters in compilation time. That is, you don’t need to write them again and will not even see them, but they will be there and working just fine.

This project is called Lombok, which is, by the way, the name of an Indonesian island. It was created by some guys and licensed under the MIT license. Importing the project is pretty straightforward, using maven or ant. So I will focus on using it. The library is powerful and has many features, but this post aims the very basic use.

The Data Annotation

The simplest way of populating a class with getters and setters is by using the annotation @Data at the class level.

It will not only generate the getters and setters methods of your class but also the methods equals(), hashCode() and toString() automatically.

It’s also possible to choose the methods and attributes to be generated by Lombok, by using the annotations @Getter and @Setter in the field level, preventing unnecessary fields to be exposed.


Easy? I’m pretty sure that, if you didn’t know Lombok, you will now start using it, because it’s just necessary to increase performance while coding. To know more about the project and its features, please visit their website.

Good luck!

Tags: java  productivity 
comments powered by Disqus