Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using an <code>enum</code> to hold configurable values like this looks like an entirely wrong design. Enums are singletons, so effectively you can only have one configuration active at any given time.</p> <p>An <code>EnumMap</code> sounds more like what you need. It's external to the <code>enum</code>, so you can instantiate as many configurations as you need.</p> <pre><code>import java.util.*; public class EnumMapExample { static enum Options { DEBUG, ALWAYS_SAVE, THREAD_COUNT; } public static void main(String[] args) { Map&lt;Options,Object&gt; normalConfig = new EnumMap&lt;Options,Object&gt;(Options.class); normalConfig.put(Options.DEBUG, false); normalConfig.put(Options.THREAD_COUNT, 3); System.out.println(normalConfig); // prints "{DEBUG=false, THREAD_COUNT=3}" Map&lt;Options,Object&gt; debugConfig = new EnumMap&lt;Options,Object&gt;(Options.class); debugConfig.put(Options.DEBUG, true); debugConfig.put(Options.THREAD_COUNT, 666); System.out.println(debugConfig); // prints "{DEBUG=true, THREAD_COUNT=666}" } } </code></pre> <h3>API links</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/EnumMap.html" rel="nofollow noreferrer"><code>java.util.EnumMap</code></a> <blockquote> <p>A specialized <code>Map</code> implementation for use with <code>enum</code> type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.</p> </blockquote></li> </ul>
 

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