What is the @Data annotation in Lombok?
What is Lombok?
Project Lombok is a Java library that helps reduce
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()andhashCode()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>Code explanation
- Line 1: We import the
Dataannotation. - Lines 5–9: We define a
Personclass withageandnameas the fields of the class. This class is annotated with the@Dataannotation. - Line 12: We create an instance of the
Personclass. The value for thenamefield is passed, because it’s a final field and the@Dataannotation includes the functionality of the@RequiredArgsConstructorannotation. - Line 13: We set the value of the
agefield of the person object, using the setter. - Line 14: We print the
agevalue of the person object, using the getter for theagefield. - Line 15: We print the
namevalue of the person object, using the getter for thenamefield. - Line 16: We print the string representation of the person object, using the
toStringmethod.