Post Format

lombok, make this mess begone

Getters and setter used to be a real pain to write, nowadays you usually get them inserted into your code from your IDE. But Java can do better – and lombok is the tool to step in.

Back in the times when Java coding was done in a plain text editor, sticking to the JavaBean convention to hide your precious secret member variables away with getters and setters was more self-torture than coding fun. The eclipse project made many things easier in that respect: automatic java code generation for getters/setters in a gorgeous IDE reduced the pain of keeping your members private to a mere nuisance. But then again – wouldn’t it be cool to have all your accessors available without generating them? Or even without seeing them in your code? Or seen from a second perspective: what is the point of having code in your project that can literally be generated by a machine anyway because it’s just the same pattern over and over again? Even C# (starting with version 3.0) does better here: Auto-Implemented Properties solve exactly this problem in a clean and elegant fashion.

lombok to the rescue

This is where project lombok tries to step in. It mixes automagic background code generation with a couple of annotations to keep you in control – and this is all it takes to spare you the incredibly boring task of typing getters, setters, .equals(), .hashCode() and more. It can also take care of closing your resources – much like C#’s using statement – a feature that will be obsolete in Java 7, but can make your life easier until then. To achieve this, a number of annotations are provided in the single JAR file:

@Getter / @Setter

Let me give you just one example of how to use them:

public @Data class Contact
private String firstName;
private String lastName;
private String country;
private String city;
private String zip;
private String street;

Notice the @Data annotation in the class definition? That is all that it takes! In my opinion this is so much closer to what my code should look like than having at least 24 java coding style lines of getters and setters. The nice thing is: lombok actually generates these methods following standard JavaBean conventions and embeds them in the bytecode – so you can access them just as if you wrote them yourself (or had eclipse generate the source code for you). One consequence: writing data classes in java that can be serialized via JAXB or JSON has never been easier!

Making all this magic work in your eclipse projects is also dead simple: the lombok.jar contains an installer with a pretty GUI that starts up when you run the JAR as an executable. Just point the installer to your eclipse installation and it will take care of the rest. Afterwards you add the JAR to your projects’ buildpath and there you go: getters and setters magically popping up in eclipse’s “Outline View” once you type in the magic annotations!

lombok and maven

If you are a maven user and you can skip the step of manually including the lombok JAR by using the following snippets in your pom.xml:



If this made you curious and you want to know more about project lombok then be sure to check out the demonstration video on the project’s homepage!

Happy (not-so-much-anymore) coding,



Leave a Reply