Java: Enums

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

You can access enum constants with the dot syntax:

Level myVar = Level.MEDIUM;

Loop Through an Enum

The enum type has a values() method, which returns an array of all enum constants.

for (Level myVar : Level.values()) {
  System.out.println(myVar);
}

// Outputs
LOW
MEDIUM
HIGH

 

posted @ 2022-11-27 07:00  小白冲冲  阅读(29)  评论(0)    收藏  举报