Skip to content

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 case values 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 printDay that takes a Day argument and prints it to the screen with System.out.println.
  • If the day is FRIDAY, SATURDAY, or SUNDAY, print the message "Weekend!"
  • Call the method from run() with several Day values.

Prev -- Up -- Next