How do you use Java Enumerations?

The usage method of Java Enumerations is as follows:

  1. a set of predefined constant values that represent a set of possible options
public enum Weekday {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
  1. Citing enumeration values: You can refer to enumeration values by using the enumeration class name followed by the enumeration value. For example:
Weekday day = Weekday.MONDAY;
  1. What is your name?
String name = day.name();  // 返回 "MONDAY"
  1. Can you please explain the process in a simpler way?
if (day == Weekday.MONDAY) {
    // 执行某些操作
}
  1. One way to use enumeration is by defining methods within the enum class, and then calling these methods on the enum values. For example:
public enum Weekday {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;

    public boolean isWeekend() {
        return this == SATURDAY || this == SUNDAY;
    }
}

Weekday day = Weekday.SATURDAY;
boolean isWeekend = day.isWeekend();  // 返回 true

Enumerations also support other features such as defining fields and constructors on enumeration values, as well as implementing interfaces.

bannerAds