# Lombok: Simplify your development of Java code

### Lombok Reference Guide

Lombok is one of my favorite Java library which is designed to save you quite a bit of coding on stuff that can get quite repetitive/boilerplate. Lombok is there to the rescue! This is more of a reference guide/cheat sheet for me to come and take a look on some of the usage. I always wanted a single page that has a good summarization of annotations I often use:

Project Lombok will reduce quite a bit of code by introducing simple annotations.

Let me describe each features of, getters/setters, override equals, hashCode, toString and/or offer a copy constructor.

By annotating @Getter or @Setter lets lombok generate default getter/setter automatically. It automattically creates with the name: getField, setField. Below are the comparisons:

Plain Vanilla Java:

With Lombok:

Result: 42 lines vs 26 lines

Using Lombok, you can annotate with a @ToString and Lombok will override our toString method and print out the classes fields.

Plain Vanilla Java:

With Lombok:

Result: 30 lines vs 24 lines

As you would already know, equals and HashCode come in as a package and they definitely should be implemented together. With this annotation, Lombok will generate both equals and hashCode methods at compile time. Please keep in mind this auto-generated codes will create the basic equals and hashcode. If you need to apply some customized behaviors to it, I would encourage you to write your own methods to handle it. Comparison

Plain Vanilla Java:

With Lombok:

Result: 72 lines vs 25 lines

@NoArgsConstructor will generate a constructor with no parameters. Just like how Object class would generate one for you. If not possible due to final variables, then compile error will happen. You can do something like @NoArgsConstructor(force = true) which will default the final values to default values (0 / false / null).

Plain Vanilla Java:

With Lombok:

Result: 20 lines vs 16 lines

@AllArgsConstructor basically means to use every variable defined to be a base constructor. It’d be easier if you just read examples below :p

Plain Vanilla Java:

With Lombok:

Result: 52 lines vs 38 lines

@Data annotation is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together. I will use an example above to show you how it can further reduce:

Remember we had an example above in Lombok:

Now if we used, @Data instead,

Result: 16 lines further reduced to 11 lines of code

As you can see, Lombok can reduce quite a bit of line of code by simply applying annotations. I personally think Lombok is a must for Java codes. You might feel it is hackish at first, but you will soon understand how Lombok simplifies your world of Java and modernizes Java.