Skip to content

Loop Through an Java Enum Example

Ramesh Fadatare edited this page Jul 12, 2019 · 1 revision

The enum type has a values() method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum.

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

public class MyClass { 
  public static void main(String[] args) { 
    for (Level myVar : Level.values()) {
      System.out.println(myVar);
    }
  } 
}

Output:

LOW
MEDIUM
HIGH
Clone this wiki locally