bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Loop Through an Enum

Loop Through an Enum

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:

Example

enum Level {
  LOW, MEDIUM, HIGH
}
public class Main {
  public static void main(String[] args) {
    for (Level myVar : Level.values()) {
      System.out.println(myVar);
    }
}
}

Previous

Java How To Loop Through a HashMap

Next chapter

Java Reference

Start with Java Keywords