How to Iterate over Java Enum : values() Example

Enum Values() Example
In this Java programming tutorial, we will learn how to iterate over enum in Java. Since Enums are a collection of a finite number of well-known objects, often we need to iterate over them. Enums are also final in Java and has a private constructor, which means you can not create enum instances once declared. Iteration over Enum is extremely simple, provided you know about implicit values() method, which is a static method, provided by java.lang.Enum. Since every enum in Java extends java.lang.Enum, they all get this implicit values() method. Actually there are couple of them, e.g. valueOf(), name(), ordinal() etc. This method returns enum constants in the order they are declared in Enum class. Even the ordinal() method returns the same order.

By the way, In last couple of Java tutorials on enum we have seen How to convert Enum to String in Java, Enum valueOf Example and Enum Constructor Example.

This tutorial focuses on iteration over Enum in Java. Remember iteration, traversing or looping over enum is same thing, so you can use this technique for any of these purpose.

How to loop over Enum in Java

How to loop over Enum in Java with ExampleIn this Java program, we have created an enum called Language, which represent programming language and there rank. Our task is to iterate through this enum and print name, and rank of each enum instance.

Just remember that, all enum in Java provides values() method, which returns an array of enum, containing all instances. Here is full code example of Iterating over enum :

 * Java program to iterate over enum using for loop and values method.
 * values() method of enum returns all enum instances as array for iteration.
 * @author java67
public class EnumHowTo {

    public enum Language{
        JAVA(1), PYTHON(2), PERL(3), SCALA(4);
        private int rank;
        private Language(int rank){
            this.rank = rank;
        public int getRank(){
            return rank;
    public static void main(String args[]) {

        System.out.println("Java Enum Iterate Example using for loop");
        for(Language pl : Language.values()){
            System.out.println( + " : " + pl.rank);

Java Enum Iterate Example using for loop
JAVA : 1
PERL : 3

That's all on How to iterate over enum in Java. As I said earlier and you can see from this code example, its simple. Don't forget values() method, which return array of enum instances. To learn more about Enum in Java and there usages, see following tutorials :
  1. Enum Fundamental and Concepts in Java
  2. Why Enum Singletons are better in Java
  3. How to create thread safe Singleton in Java using Enum
  4. Autoboxing, Generic, varargs and Enum in Java
  5. How to use Generic to write type safe classes in Java


  1. why you are maintaing 2 blogs...javarevisited and java67? if u can consolidate everything in one it will be useful for the readers

  2. Couple of More articles on Java Enum :
    10 Enum Examples in Java

    15 Java Enum Interview Questions.

    Both of them provide good overview of different enum features.