Java / Lombok Interview questions
What is @Data used for?
@Data is a convenience annotation that bundles several others together:
@Getter, @Setter, @ToString, @EqualsAndHashCode, and
@RequiredArgsConstructor, all applied at once with a single annotation on the class.
@Data public class Person { private final String id; private String name; private int age; } // equivalent to individually applying @Getter, @Setter (on non-final fields), // @ToString, @EqualsAndHashCode, and @RequiredArgsConstructor
It's a fast way to create a typical mutable data class, but it's an all-or-nothing bundle — if you
need to customize or exclude just one piece of that behavior (say, excluding a field from
equals), you generally either add the individual exclusion annotations alongside
@Data, or drop down to the individual annotations instead of the bundle.
More Related questions...