Home › Java Tutorial › Java enums
Java enums
A fixed set of named values.
An enum defines constants such as days or levels. values() returns all of them and ordinal() gives the position. Enums work nicely in switch.
Example code
enum Level { LOW, MEDIUM, HIGH }
public class Main {
public static void main(String[] args) {
Level l = Level.MEDIUM;
System.out.println(l);
System.out.println(l.ordinal());
for (Level x : Level.values()) {
System.out.println(x);
}
}
}