How to Define Properties for Enum Elements

I read the question Enum difference between java and C ++? but I'm still confused.

I want the following to return the associated string:

public enum Checker { EMPTY ("Empty"), RED ("Red"), YELLOW ("Yellow"); } 

From what I read, it should be possible. I just would like you to shed light on how to implement it.

+41
java enums
Jun 16 2018-10-16
source share
2 answers

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() // the compiler will happily accept this } 

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(){ // defensive copy, because the original list is mutable return new ArrayList<String>(members); } private Band(String... members){ this.members=Arrays.asList(members); } } 

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; } } 
+82
Jun 16 '10 at 2:36 p.m.
source share
β€” -

If the template is saved, this also works and eliminates repetition:

 public enum Checker { EMPTY, RED, YELLOW; public String getDescription(){ String name = name(); return ""+Character.toUpperCase(name.charAt(0)) +name.substring(1).toLowerCase(); } } 
+5
Jun 16 2018-10-16
source share



All Articles