Using
Access an enum constant using dot notation.
-
Declare variables with the type of the
enum. -
Assign the variable by referring to one of the constants.
Color c = Color.RED;
if¶
We can use enums in if statements with ==.
if(c == Color.RED) {
//...
}
switch¶
We can use enums in switch statements.
- The
casevalues are the constants - they do not need dot notation.
switch (c) {
case RED:
// ...
}
Methods¶
When defining a method that takes an enum, use the type of the enum for the parameter type.
public void displayRGBValue(Color c) {
// ...
}
Call the method by passing one of the constant values.
Color c = Color.RED;
displayRGBValue(Color.BLUE);
displayRGBValue(c);
Drill¶
EnumeratedTypes/com.example.enums.drills.DayDrills
- Create a method called
printDaythat takes aDayargument and prints it to the screen withSystem.out.println.- If the day is
FRIDAY,SATURDAY, orSUNDAY, print the message "Weekend!"- Call the method from
run()with severalDayvalues.