Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>"Classic" java always used "static final int ANIMAL_CAT = 0;" in cases like this.</p> <p>JDK 1.5 introduced "type-safe enums":</p> <p><a href="http://www.javapractices.com/topic/TopicAction.do?Id=1" rel="noreferrer">http://www.javapractices.com/topic/TopicAction.do?Id=1</a></p> <p>You can now do this (a very common practice):</p> <pre><code>enum Quark { /* * These are called "enum constants". * An enum type has no instances other than those defined by its * enum constants. They are implicitly "public static final". * Each enum constant corresponds to a call to a constructor. * When no args follow an enum constant, then the no-argument constructor * is used to create the corresponding object. */ UP, DOWN, CHARM, STRANGE, BOTTOM, TOP } </code></pre> <p>Or you can do this:</p> <pre><code>/** * Example 2 - adding a constructor to an enum. * * If no constructor is added, then the usual default constructor * is created by the system, and declarations of the * enum constants will correspond to calling this default constructor. */ public enum Lepton { //each constant implicity calls a constructor : ELECTRON(-1, 1.0E-31), NEUTRINO(0, 0.0); /* * This constructor is private. * Legal to declare a non-private constructor, but not legal * to use such a constructor outside the enum. * Can never use "new" with any enum, even inside the enum * class itself. */ private Lepton(int aCharge, double aMass){ //cannot call super ctor here //calls to "this" ctors allowed fCharge = aCharge; fMass = aMass; } final int getCharge() { return fCharge; } final double getMass() { return fMass; } private final int fCharge; private final double fMass; } </code></pre> <p>Here's the official documentation:</p> <p><a href="http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html" rel="noreferrer">http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html</a></p>
 

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