Enums
An enumeration is created using the enum keyword.
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
1. The identifiers Monday, Tuesday, and so on, are called enumeration constants.
2. Each is implicitly declared as a public, static member of Week.
3. These constants are called self-typed.
You declare and use an enumeration variable in much the same way as the primitive types.
Week aWeekDay;
Because aWeekDay is of type Week,
enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}
1. The identifiers Monday, Tuesday, and so on, are called enumeration constants.
2. Each is implicitly declared as a public, static member of Week.
3. These constants are called self-typed.
You declare and use an enumeration variable in much the same way as the primitive types.
Week aWeekDay;
Because aWeekDay is of type Week,
Enum With Switch
enum Size {
S, M, L
}
public class Main { public static void main(String[] args) {
Size size = Size.M;
switch(size){
case S:
System.out.println("S");
break;
case M:
System.out.println("M");
break;
case L:
System.out.println("L");
break;
} } }
S, M, L
}
public class Main { public static void main(String[] args) {
Size size = Size.M;
switch(size){
case S:
System.out.println("S");
break;
case M:
System.out.println("M");
break;
case L:
System.out.println("L");
break;
} } }