Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Inside an instance method, you can refer to the "current <code>enum</code> object" as simply <code>this</code>. It works this way because <code>enum</code> constants are actual objects, i.e. instances of a <code>class</code> -- a very special type of <code>class</code>, but a <code>class</code> nonetheless. (Note that I mentioned that these are <code>enum</code> constants: it is the convention in Java to use all uppercase letters when naming constants.)</p> <p>Your usage of <code>values()</code> is also very peculiar (not to mention that it'll perform horribly since a new array must be constructed at each call). Perhaps you'd want to take a look at <a href="http://java.sun.com/javase/6/docs/api/java/util/EnumMap.html" rel="nofollow noreferrer"><code>EnumMap</code></a>, which is a special kind of <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html" rel="nofollow noreferrer"><code>Map</code></a> optimized for <code>enum</code> constants as keys.</p> <p>If you're trying to mutate fields contained in these <code>enum</code> constants, then you should seriously consider a redesign. You should generally minimize mutability anyway, but having these <code>static</code> singletons be mutable does not sound like a good design. Instead of having these mutable fields intrinsic within the <code>enum</code> constants themselves, a <code>Map</code> from the constants to these mutable values would be a much better design.</p> <h3>See also</h3> <ul> <li><a href="https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html" rel="nofollow noreferrer">Java Tutorials/<code>enum</code></a></li> <li><em>Effective Java 2nd Edition</em> <ul> <li>Item 15: Minimize mutability</li> <li>Item 31: Use instance fields instead of ordinals</li> <li>Item 32: Use <a href="http://java.sun.com/javase/6/docs/api/java/util/EnumSet.html" rel="nofollow noreferrer"><code>EnumSet</code></a> instead of bit fields</li> <li>Item 33: Use <a href="http://java.sun.com/javase/6/docs/api/java/util/EnumMap.html" rel="nofollow noreferrer"><code>EnumMap</code></a> instead of ordinal indexing</li> </ul></li> </ul> <h3>Various questions on Java <code>enum</code></h3> <ul> <li><a href="https://stackoverflow.com/questions/2902113/configurable-values-in-enum">Configurable Values in Enum</a> </li> <li><a href="https://stackoverflow.com/questions/3029850/how-to-efficiently-use-enum-objects-as-keys-in-the-map-data-structure">How to efficiently use Enum objects as keys in the Map data structure?</a></li> <li><a href="https://stackoverflow.com/questions/2765687/is-there-a-a-c-like-way-to-get-item-number-from-enum-in-java">Is there a a C-like way to get item number from enum in java ?</a> (yes, but why would you???)</li> <li><a href="https://stackoverflow.com/questions/2780129/get-enum-by-its-inner-field">Get enum by its inner field</a></li> </ul>
    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.
    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