Java Reference ยท Types and Literals
Java enum types
Reference for Java enum types: constants, fields, constructors, abstract methods, values(), valueOf(), EnumMap, EnumSet and switch usage.
An enum type is a class with a fixed, named set of instances. Each constant is created once, when the enum class is initialized, and can never be created again. That makes enums the right tool for things like days of the week, states of a state machine or log levels, where a String or an int constant would let invalid values slip in.
Because an enum is a real class, its constants can carry fields, run constructor code and override methods. Every enum implicitly extends java.lang.Enum, which supplies name(), ordinal(), compareTo() and friends. The keyword itself is covered in enum; this page looks at the type's behavior.
enum Level { LOW(1), MEDIUM(5), HIGH(10); private final int weight; Level(int weight) { this.weight = weight; } int weight() { return weight; } }
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
[modifiers] enum Name [implements Interface, ...] { CONSTANT [(args)] [{ body }], ...; // fields, constructors, methods }
| Member | Description |
|---|---|
static E[] values() |
Generated. A new array of all constants in declaration order. |
static E valueOf(String name) |
Generated. The constant with exactly that name. Throws IllegalArgumentException if there is none and NullPointerException for null. |
String name() |
The identifier as declared. Final. |
int ordinal() |
Zero-based position in the declaration. |
String toString() |
Returns name() unless overridden. |
int compareTo(E other) |
Compares by ordinal. |
Class<E> getDeclaringClass() |
The enum class, even for constants with bodies. |
Usage notes
- Constructors are implicitly private. You cannot call
newon an enum, and a constructor cannot bepublicorprotected. - Compare with
==. There is exactly one instance per constant, so==is safe, null-safe on the left side and as fast as it gets.equals()andhashCode()are final and use identity. - The constant list comes first and ends with a semicolon if anything else follows it.
- Constants with bodies are anonymous subclasses, so
getClass()may differ fromgetDeclaringClass(). Use the latter. - Enums can implement interfaces but cannot extend classes. They are implicitly
finalunless a constant has a body, in which case they are implicitly sealed. values()allocates a copy every call. Cache it in a static field if you call it in a hot loop.- Do not rely on
ordinal()for persistence. Reordering or inserting constants changes it. Storename()or an explicit code instead. EnumMapandEnumSetare array and bit-vector based, so they are faster and more compact thanHashMapandHashSetfor enum keys, and iterate in declaration order.- Switch works on enums; with a switch expression the compiler can check that every constant is covered when you leave out
default. - Serialization and
valueOfare by name, so renaming a constant breaks stored data. - Static fields are not accessible from an enum constructor (the constants are created first). Put shared lookup maps in a nested holder class or a static initializer.