In Java, enum
(short for enumeration) is a special data type used to define a fixed set of constant values. It improves code readability, type safety, and prevents invalid values. Enums are commonly used for predefined options, like days of the week, states, or directions.
Example:
java
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
You can use it like:
java
Day today = Day.MONDAY;
if (today == Day.MONDAY) {
System.out.println("Start of the week!");
}
Enums can include fields, methods, and constructors, making them more versatile than traditional constants.