Junior — Middle
Can you explain the purpose of the @Data annotation from Lombok and how it simplifies class development in Java?
sobes.tech AI
Answer from AI
The @Data annotation from the Lombok library automatically generates the following methods for a class:
- Getters and setters for all fields,
toString(),equals(), andhashCode()methods,- A default constructor,
- A constructor with parameters for all final fields.
This significantly reduces the boilerplate code typically written manually in Java classes for data storage (POJO). Thanks to @Data, the code becomes cleaner and easier to maintain.
Example:
import lombok.Data;
@Data
public class User {
private String name;
private int age;
}
In this example, Lombok will generate all the necessary methods, and the developer does not need to write them manually.