Java Reference ยท Keywords and Statements
The enum keyword
Reference for the Java enum keyword: declare constants with fields and methods, use values(), valueOf(), ordinal(), switch, EnumMap and EnumSet.
An enum declares a type with a fixed set of named instances. Each constant is a singleton object created when the enum class is initialized. Because the set is closed, the compiler can check switch statements for completeness and you can never get an invalid value.
Enums are full classes: they can have fields, constructors, methods, implement interfaces, and give each constant its own body. They cannot extend another class (they implicitly extend java.lang.Enum) and cannot be extended.
enum Level { LOW, MEDIUM, HIGH } Level l = Level.valueOf("HIGH");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
[public] enum Name [implements Iface] { CONSTANT_ONE [(args)] [{ body }], CONSTANT_TWO; // semicolon only if members follow // fields, constructors (implicitly private), methods }
| Member | Description |
|---|---|
static E[] values() |
A new array of all constants in declaration order. |
static E valueOf(String name) |
The constant with that exact name. Throws IllegalArgumentException if none; NullPointerException for null. |
String name() |
The declared identifier. Final. |
int ordinal() |
Zero-based position. Avoid storing it; reordering changes it. |
String toString() |
Returns name() unless overridden. |
int compareTo(E o) |
Compares ordinals. |
Usage notes
- Comparison: use
==; constants are unique, so==is correct and does not throw onnullthe way.equals()on a null variable would. - Constructors are implicitly
private; you cannot callnewon an enum. - Switch: switch expressions over an enum are checked for exhaustiveness at compile time; adding a constant later turns a missing case into a compile error. A
switchstatement is not forced to be exhaustive unless it uses patterns. - EnumMap and EnumSet store values in arrays/bit sets and are faster and smaller than
HashMap/HashSetfor enum keys. Iteration follows declaration order. - Mutable fields are legal but dangerous, since constants are shared global state. Keep fields
final. - Singletons and strategies: a single-constant enum is a robust singleton; constant bodies implement a strategy per constant.
- Serialization and reflection: enums serialize by name and cannot be cloned or created reflectively, which keeps them unique.
- Interfaces: an enum can implement an interface, which lets you share an API across enums;
sealedinterfaces combine well with enums and records. - Static initialization: constants are created first; a constructor cannot read a static non-constant field of its own enum.
Related
switch,class,interface,record- Lessons: Enums, Pattern matching, Specialized collections