Short answer
You need a constructor, a field, and a receiver.
Constructors
Enumeration types can have constructors provided that their access level is either private or default (private-package). You cannot directly call these constructors except the enumeration declaration itself. Like classes, when you define an enum constant without parameters, you actually call the default constructor generated by the compiler. For example.
public enum King{ ELVIS }
equivalently
public enum King{ ELVIS()
And just like in classes, if you define an explicit constructor, the compiler will not insert the default constructor, so it will not compile:
public enum King{ ELVIS, // error, default constructor is not defined MICHAEL_JACKSON(true) ; private boolean kingOfPop; King(boolean kingOfPop){this.kingOfPop = kingOfPop;} }
This is a pretty good listing reference , which also explains the problems with the constructors.
Fields and accessories
Enumerations are constants and are immutable as such. However, they can define fields that may have state. This is a terrible practice, because developers expect enums and their associated values ββto be constants, but you can still define a non-final field in an enumeration using getters and setters.
This is Java legal code:
public enum Color{ RED("FF0000"), GREEN("00FF00"), BLUE("0000FF"); private String code; public String getCode(){return code;} public void setCode(String code){this.code = code;} private Color(String code){this.code = code;} }
But it allows evil code as follows:
String oldBlue = Color.BLUE.getCode(); Color.BLUE.setCode(Color.RED.getCode()); Color.RED.setCode(oldBlue);
So, in 99.99% of cases: if you have fields in your enumerations, you should make them final and provide only getters. If the fields are not immutable, provide protective copies:
public enum Band{ THE_BEATLES("John","Paul","George","Ringo"); private final List<String> members; public List<String> getMembers(){
Decision
In your case, this is very simple: you just need one field of type string (immutable), so initializing it in the constructor and providing getter is quite normal:
public enum Checker { EMPTY ("Empty"), RED ("Red"), YELLOW ("Yellow"); private final String value; private Checker(final String value) { this.value = value; } public String getValue() { return value; } }