Enumerations in Java


Enum Enumerate JavaViews 1567

Enum in Java is the short form of Enumeration. The Java Enum class defines a list of constants whose values we cannot change. It is similar to the final variable. We always define the Enum constants in the upper case. In this article, we will see how to enumerate in Java.

Enumerations in Java

Syntax of declaring an Enum (Enumeration)

To declare an enum we must use the enum keyword followed by the variable. Inside the curly braces, we declare the constants separated by a comma.

enum Speed {
  SLOW, MEDIUM, FAST
}

Speed – represents the Enum variable name

The values inside the curly braces represent the Enum constants.

Features of Enum (Enumeration)

  • We must always define the enum constants using upper case
  • We can declare an enum either inside or outside the class
  • Enum is a class and every constant is an object of an enum type.
  • We can declare fields and methods inside an enum
  • Each enum constant is separated by a comma and should end with a semicolon if an enum contains methods as well.

Accessing Enum constants in Java

We can access enum constants in 2 different ways:

  • Creating an enum variable: We can create an enum variable by assigning the enum constant to it. We can use this variable to print the value.
    enum Speed {
        SLOW, MEDIUM, FAST
      }
    Speed s = Speed.MEDIUM;
    System.out.println(d);
  • Using enum name: We can directly access the constant using the enum name.
    enum Speed {
        SLOW, MEDIUM, FAST
      }
    System.out.println(Speed.SLOW);
    
    

Using the main method inside an Enum in Java

If we use the main method inside an Enum, we can execute the Enum directly without any class.

public enum Speed {
  SLOW, MEDIUM, FAST;
  
  public static void main(String[] args) {
    System.out.println(Speed.MEDIUM);
    System.out.println(Speed.FAST);
  }
}
MEDIUM
FAST

Defining Java Enum outside a class

We can declare an enum outside a class as you can see in the below example.

enum Speed {
  SLOW, MEDIUM, FAST
}
public class Sample {

  public static void main(String[] args) {
    Speed s = Speed.SLOW;
    System.out.println(d);

  }

}
SLOW

Defining Java Enum inside a class

Below is an example of declaring an enum inside a class.

public class Sample {

  enum Speed {
    SLOW, MEDIUM, FAST
  }
  public static void main(String[] args) {
    Speed s = Speed.MEDIUM;
    System.out.println(d);

  }

}
MEDIUM

Enum using if-else statement

We can also use the enum variable in the if-else conditional statement as in the below example.

enum Speed {
  SLOW, MEDIUM, FAST
}
public class ConditionalEnum {

  public static void main(String[] args) {
    Speed s = Speed.MEDIUM;
    if(s == Speed.SLOW)
      System.out.println("Vehicle speed is slow");
    else if(s == Speed.MEDIUM)
      System.out.println("Vehicle speed is medium");
    else
      System.out.println("Vehicle speed is fast");

  }

}
Vehicle speed is medium

Enum within a switch statement

Below is an example of using enum variables inside a switch statement by passing as an argument. You might be interested in reading switch case in Java.

enum Speed {
  SLOW, MEDIUM, FAST
}
public class ConditionalEnum {

  public static void main(String[] args) {
    Speed s = Speed.MEDIUM;
    
    switch(s) {
    case SLOW:
      System.out.println("Speed is Slow");
      break;
    case MEDIUM:
      System.out.println("Speed is Medium");
      break;
    case FAST:
      System.out.println("Speed is Fast");
      break;
    }

  }

}
Speed is Medium

Iterating enum values using For loop

We can iterate through enum values in Java using a for loop to retrieve and print each enum constant. It iterates through the enum constants in the same order as declared. You might be interested in reading Iterator in Java.

enum Speed {
  SLOW, MEDIUM, FAST
}
public class Sample{

  public static void main(String[] args) {

    for(Speed s : Speed.values())
      System.out.println(s);
  }

}
SLOW
MEDIUM
FAST

Enum Methods

Java Enum class has different methods to retrieve values.

values() – This method returns all the constant values declared in the enum.

valueOf() – It returns the enum constant of the specified string value.

ordinal() – Used to find the index of the enum constant which is similar to an array index.

The below example shows how to use various Java enum supporting methods to retrieve the values and their index.

public class Test {
  enum Speed {
    SLOW, MEDIUM, FAST
  }

  public static void main(String[] args) {
    //Using values() method and ordinal method
    for(Speed s : Speed.values()) {
      System.out.println("Value: " + s + " Index: " + s.ordinal());
    }
    
    //Using valueOf() method
    System.out.println(Speed.valueOf("FAST"));

    //toString method
    Speed s = Speed.SLOW;
    System.out.println(s.toString());
  }

}
Value: SLOW Index: 0
Value: MEDIUM Index: 1
Value: FAST Index: 2
FAST
SLOW

Initializing values to enum constants

We can initialize values to Java enum constants by using constructors. An enum can contain a constructor which is invoked for every enum constant. The enum constructors are private by default. Below is an example of initializing values to the Java enum constants using constructors. We can access the values using the private field which we initialize within the constructor.

public class Test {
  enum Speed {
    SLOW(15), MEDIUM(40), FAST(80);
    
    private int sp;
    private Speed(int sp) {
      this.sp = sp;
    }
  }

  public static void main(String[] args) {
    
    for(Speed s : Speed.values())
      System.out.println(s + ": " + s.sp);
    
  }

}
SLOW: 15
MEDIUM: 40
FAST: 80

Enum and methods

We can define methods inside an enum. Whenever an enum in Java has both fields and methods, we must end the constants with a semicolon towards the end. We can define normal and abstract methods inside an enum.

Below is an example of declaring methods inside an enum.

public class EnumMethods {

  enum Speed {
    SLOW(10), MEDIUM(40), FAST(80);
    
    private int sp;
    
    Speed(int sp){
      this.sp = sp;
    }
    
    public int getSpeed() {
      return this.sp;
    }
  }
  public static void main(String[] args) {
    Speed s = Speed.SLOW;
    System.out.println(s + " :" + s.getSpeed());
    Speed s2 = Speed.FAST;
    System.out.println(s2 + " :" + s2.getSpeed());

  }

}
SLOW :10
FAST :80

We have read about how to enumerate in Java. You might also be interested in EnumMap

Reference

Translate ยป