Junior — Middle
What are the differences between @Value and @Data annotations in the Lombok library?
sobes.tech AI
Answer from AI
The @Data annotation in Lombok generates for a class:
- getters and setters for all fields,
equals(),hashCode(),toString()method,- default constructor,
- and
RequiredArgsConstructor(constructor for final fields).
The @Value annotation makes a class immutable:
- all fields are
privateandfinal, - generates only getters (no setters),
- generates
equals(),hashCode(),toString(), - generates a constructor with all fields,
- the class is marked as
final.
In summary:
@Data— for mutable classes with a full set of methods.@Value— for immutable classes where fields cannot be changed after creation.
Example:
@Data
class User {
private String name;
private int age;
}
@Value
class ImmutableUser {
String name;
int age;
}