Lombok Data annotation

Lombok Data annotation @Data is a special annotation that groups functionalities from multiple annotations namely:

  1. @ToString,
  2. @EqualsAndHashCode,
  3. @Getter on all fields,
  4. @Setter on all non-final fields, and
  5. @RequiredArgsConstructor.

In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans:

  1.  getter methods for all fields,
  2. setter methods for all non-final fields,
  3.  appropriate toString(),
  4. appropriate equals() and
  5. hashCode() implementations that involve the fields of the class,
  6. and a constructor that initializes all final fields,
  7. 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 Data annotation

Without Lombok your POJO code looks like:

Copy to Clipboard

With Lombok @Data annotation

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.

Copy to Clipboard

Demo:

Copy to Clipboard

Output:

Copy to Clipboard

References: