Java Reference
In-Depth Information
public static void print(EnumSet<Day> days, String msg) {
System.out.print(msg);
for(Day d : days) {
System.out.print(d + " ");
}
System.out.println();
}
}
All days: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY
Weekdays: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY
Weekends: SATURDAY SUNDAY
Summary
Like a class and an interface, an enum defines a new reference type in Java. An enum type consists of a pre-defined,
ordered set of values, which are known as the elements or constants of the enum type. Constants of the enum type
have a name and an ordinal. You can obtain the reference of an enum constant using its name and ordinal, and vice
versa. Typically, an enum type is used to define type-safe constants.
An enum type has several things that a class has. It has constructors, instance variables, and methods. However, the
constructors of an enum type are implicitly
private
. An enum type can also implement interfaces just as a class can.
You can declare a variable of the enum type. The variable can be assigned
null
or one of the constants of the
enum type. Every enum type is implicitly inherited from the
java.lang.Enum
class. An enum type can be used in
the switch statements. Java provides an efficient implementation of an
EnumSet
class to work with a range of enum
constants of a specific enum type.