Lombok Data annotation @Data is a special annotation that groups functionalities from multiple annotations namely:
- @Getter on all fields,
- @Setter on all non-final fields, and
In other words,
@Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans:
- getter methods for all fields,
- setter methods for all non-final fields,
appropriate equals() and
hashCode() implementations that involve the fields of the class,
- and a constructor that initializes all final fields,
- as well as all non-final fields with no initializer that have been marked with
@NonNull, in order to ensure the field is never null.
Lombok annotation @Data simply tells to the IDE (either Eclipse or Spring tool Suite or IntelliJ etc), to generate all the boilerplate code for you silently. And this is done during compile time, so you usually don’t have to care about lombok being on your runtime path at all.
Let’s have a look into examples without Lombok Data annotation in plain vanilla Java code and with @Data annotation .
Without Lombok your POJO code looks like:
If we use Lombok @Data annotation the POJO code looks like below. Final generated .class java byte code files contains code like getters, setters etc like above code, but that code generation will happened at compile time, Lombok library take cares of that, users can get rid of writing all that boiler plate code just by adding @Data annotation to the POJO class. See at bellow code how much code reduced for the developer if we use lombok.