Lombok @Data annotation

In this article, we will take a look at @Data annotation in lombok, its use and options with programs and explanations.
Before moving further, make sure that lombok is installed in your IDE and added to project classpath.

@Data annotation
Lombok @Data annotation is a shortcut that combines following annotations

That is, instead of applying all these annotations separately, you can write @Data. This means that @Data over a class will generate
1. toString() method.
2. getter methods for all fields and setter methods for all non-final fields.
3. equals() and hashCode() using all the fields.
4. constructor that takes all the fields annotated with @NonNull or final fields.

But, with @Data, you cannot customize the behavior of these annotations. So, if you are fine with default settings of these annotations, it is better to use @Data.
POJO without lombok
Below is a java class having two fields or instance variables, without lombok annotations.

public class User {

  private String name;

  private int age;

  public User() {

  public String getName() {
    return name;

  public void setName(String name) {
    this.name = name;

  public int getAge() {
    return age;

  public void setAge(int age) {
    this.age = age;
  public boolean equals(Object o) {
    // implementation

  public int hashCode() {
    // implementation

  public String toString() {
    // implementation

Above class with lombok @Data annotation will look like

import lombok.Data;

public class User {

  private String name; 

  private int age;

That is all. You can see how concise the class becomes.

@Data Example
Below is an example program that creates an object of the above class annotated with @Data annotation and calls its various methods.

public class DataDemo {

  public static void main(String[] a) {
    User user = new User();
    System.out.println("Name is: " +user.getName());
    System.out.println("Age is: " +user.getAge());
    System.out.println("Hashcode is: " +user.getName());
    System.out.println("User as a string: " +user.getName());

Name is: A
Age is: 22
Hashcode is: 4844
User as a string: User(name=A, age=22)

@Data configurations
1. Access modifier for all getter/setter methods generated with @Data will be public. To set any other access type, you should write explicit @Getter/@Setter with AccessLevel option.

2. equals() and hashCode() generated with @Data use all the fields of the class. To skip any field, use explicit @EqualsAndHashCode.Exclude over it.

3. @Data will not generate a constructor if there is already one present in the class.

4. @Data will not generate equals() method, if there is a method with this present in the class.

5. All transient and static fields will not be considered while generating equals() and hashCode() methods.

Hope the article was useful.

Liked the article ? Spread the word...