I have read the question Difference of Enum between java and C++? but I'm still confused.
I would like the following to return the related String:
public enum Checker { EMPTY ("Empty"), RED ("Red"), YELLOW ("Yellow"); }
From what I have read, this should be possible. Just would like you to shed some light on it on how to implement it.
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type.
An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden).
Enum constants are implicitly static and final and you can not change their value once created. Enum in Java provides type-safety and can be used inside switch statements like int variables.
You need a constructor, a field and a getter.
Enum types can have constructors, provided that their access level is either private or default (package-private). You can not directly call these constructors, except in the enum declaration itself. Similar to classes, when you define an enum constant without parameters, you actually call the default constructor generated by the compiler. E.g.
public enum King { ELVIS }
is equivalent to
public enum King { ELVIS() // the compiler will happily accept this }
And just like in classes, if you define an explicit constructor, the compiler will not insert a default constructor, so this 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 reference on enums that also explains the constructor issues.
Enums are constants and are immutable as such. They can however define fields, that can have state. This is an awful practice, because developers will expect enums and their associated values to be constants, but you can still define a non-final field in an enum with getters and setters.
This is legal java 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 enables evil code like this:
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 enums, you should make them final and provide getters only. If the fields are not immutable themselves, provide defensive copies:
public enum Band { THE_BEATLES("John","Paul","George","Ringo"); private final List<String> members; public List<String> getMembers(){ // defensive copy, because the original list is mutable return new ArrayList<String>(members); } private Band(String... members){ this.members=Arrays.asList(members); } }
In your case it's very simple: you just need a single field of type string (immutable), so initializing it in the constructor and providing a getter is perfectly ok:
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; } }
If the pattern holds, this works as well and eliminates the repetition:
public enum Checker { EMPTY, RED, YELLOW; public String getDescription(){ String name = name(); return ""+Character.toUpperCase(name.charAt(0)) +name.substring(1).toLowerCase(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With