Enum type
An Enum (short for enumeration) is a custom data type that allows us to define a fixed set of named values. In other words, it specifies exactly which values a variable of that type can take. When defining an Enum, we list all possible values inside braces. This is similar to how built-in types like int have a defined range of allowed values, but in this case, we explicitly decide what those values are.
public enum Day {
Mo, Tu, We, Th, Fr, Sa, Su
}
public class Main {
public static void main(String[] args) {
System.out.println(Day.Tu); // works like using a predefined constant instead of a raw value
Day number_of_a_day_of_the_week = Day.Mo;
System.out.println(getDay(number_of_a_day_of_the_week));
}
private static String getDay(Day x) {
return switch(x) {
case Mo -> "Monday";
case Tu -> "Tuesday";
case We -> "Wednesday";
case Th -> "Thursday";
case Fr -> "Friday";
case Sa -> "Saturday";
case Su -> "Sunday";
};
}
}