I came across a code where @Getter
was used on an enum declaration, I want to know if it's possible to use @Getter
over an enum declaration and what purpose it serves?
As far as I know, @Getter
generates getter methods on member variables. Am I missing something?
Is it possible to annotate the class with @Getter
as well?
If you check the Lombok's @Getter and @Setter Reference, you can see that:
You can also put a @Getter and/or @Setter annotation on a class. In that case, it's as if you annotate all the non-static fields in that class with the annotation.
This means when you put @Getter
in the Class/Enum level, it will generate getters
for all non static members on this Class/Enum, as if you put @Getter
for all those members repeatedly.
Example:
To illustrate this with an example both these two codes are equivalent:
Code 1:
@Getter
public enum MyEnum {
private int member1;
private int member2;
}
Code 2:
public enum MyEnum {
@Getter
private int member1;
@Getter
private int member2;
}
Here getter
methods will be generated for both members.