Java Tutorial
Java Enums
An enum names a fixed set of constants so you write Day.MON instead of magic numbers — and the compiler rejects values that are not on the list.
Named constants
An enum is a type whose allowed values you list once. Use those names in variables, comparisons, andswitch instead of 0, 1, 2 — the names document intent and typos become compile errors.
Example
enum Day { MON, TUE, WED, THU, FRI }
public class Main {
public static void main(String[] args) {
Day d = Day.TUE;
System.out.println(d); // TUE
System.out.println(d.ordinal()); // 1
}
}Compare and switch
Enum values compare with ==. A switch on an enum is especially clear: each case is a named constant, not a fragile number.
Example
enum Status { OPEN, CLOSED, PENDING }
public class Main {
public static void main(String[] args) {
Status s = Status.OPEN;
switch (s) {
case OPEN -> System.out.println("Accepting orders");
case CLOSED -> System.out.println("Come back later");
case PENDING -> System.out.println("Almost ready");
}
}
}values and ordinal
Day.values() returns every constant in declaration order. ordinal() is the zero-based index. Prefer the name in your own logic — ordinals break if someone reorders the list.
Example
enum Size { S, M, L }
public class Main {
public static void main(String[] args) {
for (Size size : Size.values()) {
System.out.println(size + " at " + size.ordinal());
}
}
}Do not store ordinal() in a database as a permanent key. Persist the enum name (size.name()) or an explicit field so reordering constants does not corrupt old data.
Try It Yourself
Exercise: Define enum TrafficLight { RED, YELLOW, GREEN }. Set a variable to YELLOW and print a message for each color with if or switch.
Show solution
enum TrafficLight { RED, YELLOW, GREEN }
public class Main {
public static void main(String[] args) {
TrafficLight light = TrafficLight.YELLOW;
if (light == TrafficLight.RED) System.out.println("Stop");
else if (light == TrafficLight.YELLOW) System.out.println("Slow");
else System.out.println("Go");
}
}The enum limits the variable to three values; comparing with == is the usual pattern.
Key Takeaways
- An
enumlists a fixed set of named constants as a type. - Use names in variables and
switchinstead of magic numbers. values()iterates every constant;ordinal()is the index — prefer names.- Invalid values are rejected at compile time, not discovered later as a wrong
int.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Named phase states
Solid, liquid, gas are a closed set. An enum names them so you cannot invent a fourth phase by typo.
Example
enum Phase { SOLID, LIQUID, GAS }
public class Main {
public static void main(String[] args) {
Phase p = Phase.LIQUID;
System.out.println(p);
}
}Biology
Base enum
DNA uses four bases. Enum constants keep A, C, G, T as a type, not free strings.
Example
enum Base { A, C, G, T }
public class Main {
public static void main(String[] args) {
Base b = Base.G;
System.out.println(b.ordinal());
}
}