1. Enum, enum keyword, equivalent to public final static.
2. For example:
An enumeration type named spiciness is defined first.
Public enum spiciness {Not , MILD, MEDIUM, Hot, flaming}
To test the enum again, this test method shows that it has the ToString () method, which conveniently displays the name of an enum instance.
Public class Simpleenumuse { publicstaticvoid main (string[] args) { = Spiciness.medium; System.out.println (Howhot); }}
Output Result: MEDIUM
In addition, Enum also has the ordinal () method, which makes it easy to represent the order of declarations for a particular enum constant.
Public class Enumorder { publicstaticvoid main (string[] args) { for (spiciness s:spiciness.values ()) { + ", ordinal" + s.ordinal ()); }}}
Output Result:
Not, ordinal 0
MILD, ordinal 1
MEDIUM, ordinal 2
Hot, ordinal 3
Flaming, ordinal 4
A brief introduction to <java> enumeration