Sub topic 1
Your First Enum: Named Constants Done Right
Many programs need a value that can only be one of a few fixed choices. A day of the week is a good example. There are seven days, and there will never be an eighth. Java gives you a special type for this kind of value, called an enum.
The Problem with Plain Numbers
Before enums existed, programmers often used numbers to stand for choices:
public class OldWay {
static final int MONDAY = 0;
static final int TUESDAY = 1;
public static void main(String[] args) {
int today = 42; // Compiles, but 42 is not a day!
System.out.println(today);
}
}The compiler cannot stop you from using 42 as a day, because to Java it is just an int. Printing today also shows a number, not a name. Enums fix both problems.
Declaring an Enum
An enum (short for "enumeration") is a type whose values are a fixed list of named constants. A constant is a value that never changes. You declare an enum with the enum keyword and list its constants, separated by commas:
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }Each name, such as MONDAY, is one constant of type Day. By convention, constant names are written in capital letters. A variable of type Day can only hold one of these seven constants, or null. If you write Day today = 42;, the program will not compile.
Built-in Enum Methods
Every enum gets some useful methods for free. You do not have to write them yourself.
| Method | What it does | Example result |
|---|---|---|
| `values()` | Returns an array of all constants, in declared order | `[MONDAY, TUESDAY, ...]` |
| `name()` | Returns the constant's name as a `String` | `"FRIDAY"` |
| `ordinal()` | Returns the constant's position, starting at 0 | `4` for `FRIDAY` |
| `valueOf(String)` | Finds the constant with that exact name | `Day.FRIDAY` |
This program uses all four methods. Save it as Main.java, then run it with java Main.java:
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
public class Main {
public static void main(String[] args) {
for (Day d : Day.values()) {
System.out.println(d.ordinal() + " " + d.name());
}
Day friday = Day.valueOf("FRIDAY");
System.out.println(friday == Day.FRIDAY); // true
}
}The output starts with 0 MONDAY and ends with 6 SUNDAY, followed by true.
Comparing Constants with ==
Java creates each enum constant exactly once, so there is only ever one Day.FRIDAY object. That means you can safely compare constants with ==. For enums, == is the normal, preferred way to compare.
How valueOf Finds a Constant
Exercises
Exercise
Declare an enum named Size with the constants SMALL, MEDIUM, and LARGE. What does Size.LARGE.ordinal() return?
Show answer
enum Size { SMALL, MEDIUM, LARGE }. Size.LARGE.ordinal() returns 2, because positions start at 0 and LARGE is the third constant.
Exercise
What happens when you run Day.valueOf("Monday"), and why?
Show answer
It throws an IllegalArgumentException. valueOf needs an exact, case-sensitive match, and the constant is named MONDAY, not Monday.
Exercise
Why is day == Day.SUNDAY a safe way to check whether a Day variable holds SUNDAY?
Show answer
Java creates each enum constant only once, so any variable holding SUNDAY refers to that same single object. Using == compares object identity, which is correct for enums.