What is the @Data annotation in Lombok?

What is Lombok?

Project Lombok is a Java library that helps reduce boilerplate codeA section of code that is repeated in multiple places with little variation.. Java is a verbose language where repetitive code like getters, setters, and so on can be avoided. Lombok reduces the boilerplate code with its annotations, which get plugged during the build process.

Lombok can easily be added to the project as one of the dependencies.

If the project is a Gradle project, we can add the following two lines to the dependencies section of the build.gradle file:

compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'

If the project is a Maven project, we can add the following lines to the dependencies section of the pom.xml file:

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>

@Data annotation

The @Data annotation is equivalent to the combination of the following annotations:

  • @Getter
  • @Setter
  • @RequiredArgsConstructor
  • @ToString
  • @EqualsAndHashCode

We can replace annotating a class with the annotations that are listed above and a single @Data annotation. The @Data annotation does the following work:

  • It generates the getter methods for all the fields.
  • It generates the setter methods for all the non-final fields.
  • It generates the toString() method implementation.
  • It generates appropriate equals() and hashCode() implementations, involving the fields of class.
  • It generates a constructor that initializes all the final fields, as well as all the non-final fields with no initializer that have been marked with @NonNull, in order to ensure that the field is never null.

Code example

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
@Data annotation in Lombok

Code explanation

  • Line 1: We import the Data annotation.
  • Lines 5–9: We define a Person class with age and name as the fields of the class. This class is annotated with the @Data annotation.
  • Line 12: We create an instance of the Person class. The value for the name field is passed, because it’s a final field and the @Data annotation includes the functionality of the @RequiredArgsConstructor annotation.
  • Line 13: We set the value of the age field of the person object, using the setter.
  • Line 14: We print the age value of the person object, using the getter for the age field.
  • Line 15: We print the name value of the person object, using the getter for the name field.
  • Line 16: We print the string representation of the person object, using the toString method.

Free Resources