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.
The
@Data
annotation is one of Lombok’s most powerful features.It combines several other annotations and acts as a shortcut that combines following annotations
@ToString
@EqualsAndHashCode
@Getter
@Setter
@RequiredArgsConstructor
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; @Data public class User { private String name; private int age; }
That is all. You can see how concise the class becomes.
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(); user.setName("A"); user.setAge(22); 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.
While the
@Data
annotation can significantly simplify your code, it’s essential to use it judiciously.Below are some best practices to ensure you get the most out of Lombok’s powerful capabilities:
1. Limit @Data to Simple Data Classes
Reserve the @Data
annotation for simple data classes that primarily hold state and don’t contain complex business logic.
This will help you maintain a clear separation of concerns and ensure your code remains clean and understandable.
2. Avoid Using @Data on Entity Classes
For entity classes in your data model, refrain from using @Data
to prevent unintentional exposure of sensitive information.
Instead, consider using specific annotations like @Getter
and @Setter
for fine-grained control over your entity properties.
3. Update Lombok Regularly
Lombok is actively maintained and improved by its developers.
Make sure to update to the latest version regularly to benefit from bug fixes, performance improvements, and new features.
Hope the article was useful.