Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Short Answer</h2> <p>You need a constructor, a field and a getter.</p> <h2>Constructors</h2> <p>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.</p> <pre><code>public enum King{ ELVIS } </code></pre> <p>is equivalent to </p> <pre><code>public enum King{ ELVIS() // the compiler will happily accept this } </code></pre> <p>And just like in classes, if you define an explicit constructor, the compiler will not insert a default constructor, so this will not compile:</p> <pre><code>public enum King{ ELVIS, // error, default constructor is not defined MICHAEL_JACKSON(true) ; private boolean kingOfPop; King(boolean kingOfPop){this.kingOfPop = kingOfPop;} } </code></pre> <p>This is <a href="http://download.oracle.com/javase/tutorial/java/javaOO/enum.html" rel="noreferrer">a pretty good reference on enums</a> that also explains the constructor issues.</p> <h2>Fields and Accessors</h2> <p>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 <em>can</em> still define a non-final field in an enum with getters and setters.</p> <p>This is legal java code:</p> <pre><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;} } </code></pre> <p>But it enables evil code like this:</p> <pre><code>String oldBlue = Color.BLUE.getCode(); Color.BLUE.setCode(Color.RED.getCode()); Color.RED.setCode(oldBlue); </code></pre> <p>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:</p> <pre><code>public enum Band{ THE_BEATLES("John","Paul","George","Ringo"); private final List&lt;String&gt; members; public List&lt;String&gt; getMembers(){ // defensive copy, because the original list is mutable return new ArrayList&lt;String&gt;(members); } private Band(String... members){ this.members=Arrays.asList(members); } } </code></pre> <h2>Solution</h2> <p>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:</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload