Enum is an acronym for “enumeration” which means “specific set of values”. The enums are referred to as programming constructs that are a part of most programming languages. The enums are the special types of classes in Java that are responsible for storing a finite set of values. The enums in Java are mostly used in scenarios where there is a requirement to present the entities distinctively.
In this article, we will describe in detail the implementation of enums in Java.
How to Use Enums in Java?
The enums in Java are the class type that represents a special set of predefined values. The declared variable must be equal to at least one of the already specified values. The enum is declared using the “enum” keyword along with the values declared in the uppercase format in Java. An example of enums can be seasons (SUMMER, WINTER, AUTUMN, SPRING) or days in a week.
Let us have a look at the syntax of the enum in Java
enum Days {
MONDAY, TUESDAY, WEDNESDAY
}
To access the constant from the enum we can use:
Days x= Days.MONDAY
Where x represents a variable.
The next part of the article involves explaining different ways and methods through which enums are implemented in Java.
The enums in Java are declared in two ways. One way is to declare an enum outside the class and the other is to declare the enum inside the class.
Method 1: Declaring Enum Outside the Class
The below code declares an enum with four seasons that are implemented outside the class.
//Declaration outside the class
enum Seasons {
SUMMER, WINTER, AUTUMN, SPRING
}
public class Testenum {
public static void main(String[] args)
{
Seasons s = Seasons.WINTER;
System.out.println("The Season is :"+ s);
}
}
In the above code, the enum is declared as Sesons, and the values are declared in that enum. To keep in mind the enum here is declared outside the class.
Output
In the output below, the season is declared as “WINTER”.

Method 2: Declaring Enum Inside the Class
The below code depicts the enum implemented within the class.
//Declaration of enum inside the class
public class Testenum {
enum Seasons {
SUMMER, WINTER, AUTUMN, SPRING
}
public static void main(String[] args)
{
Seasons s = Seasons.WINTER;
System.out.println("The Season is :"+ s);
}
}
In the above code, the enum is declared inside the class as Sesons, and the uppercase values are declared in the enum.
Output
The below output yields the season as “WINTER”

Example: Simple Implementation of Enums in Java
The example below depicts the implementation of enums to fetch the constant values in Java.
//Enum for months
enum Months {
JANUARY, FEBRUARY, MARCH, APRIL, MAY
}
//A class to implement the above enum
class months {
public static void main(String[] args) {
System.out.println("The Months are: ");
System.out.println(Months.MAY);
System.out.println(Months.MARCH);
}
}
In the above piece of code:
- The enum for the Months is declared.
- A class is created and the values are declared in the uppercase in that enum.
- Using the println() method the output is printed.
Output
The below output shows that the months are printed by implementing the enums in Java.

Methods of Enum Class in Java
The enum in Java comprises three methods. These methods are listed below along with the explanation and code implementation.
values(): All the values in the enum are returned using this method.
valueOf(): The valueOf() method retrieves the value of the specified enum.
ordinal(): This method returns the index of the enum specified.
Example 1: Implementation of Different Methods of enum in Java
The below code demonstrates the working of the methods of the Enum class in Java:
//Declaring a class for enum methods
class Enumexp{
public enum Months {JANUARY, FEBRUARY, MARCH, APRIL, MAY}
public static void main(String[] args) {
for (Months m : Months.values()){
System.out.println(m);
}
//The enum method valueOf() that fetches the value
System.out.println("Value of MAY is: "+Months.valueOf("MAY"));
//The enum method ordinal() gets the index of the value
System.out.println("Index of MARCH is: "+Months.valueOf("MARCH").ordinal());
}
}
Output
The below output prints the value of May and the index value of March which is 2.

Example 2: Enum Implemented Using the Switch Statement
The switch keyword in Java is used when there are plenty of choices available for the user and the user performs a task separately for each choice. The switch statement contains value and it compares these values from a list of values for equality. The switch statement is used with a break statement in the code below.
enum Direction {
//Declare the values in the enum.
EAST, WEST, NORTH, SOUTH
}
//Declare a class for the enums
class enumclass {
Direction home;
public enumclass(Direction home) {
this.home = home;
}
//Switch statements of enum.
public void dir() {
switch(home) {
case NORTH:
System.out.println("My home is in NORTH Direction");
break;
case SOUTH:
System.out.println("My home is in EAST Direction");
break;
default:
System.out.println("I am not sure about the direction of my home");
break;
}
}
}
class output {
//The object of the enum class in Java
public static void main(String[] args) {
enumclass t = new enumclass(Direction.NORTH);
t.dir();
}
}
In the above code:
- First, an enum named “direction” is declared.
- This enum consists of four values that are EAST, WEST, NORTH, and, SOUTH.
- A class is declared as enumclass along with the string “home” declared with the enum.
- In the next step, the switch statements contain the case and a break statement.
- New values are added to each switch statement to be compared with the specified value by the user and the result is printed accordingly.
Output
The output below depicts that the value NORTH is compared with the switch case values therefore, the result is returned as NORTH.

This brings us an end to the implementation of enums in Java.
Conclusion
The enums in Java are used for representing the set of constants that are related to each other. The enums are considered data types that create constants to be used with variables. These are implemented using the “enum” keyword of Java. In this article, we have applied different methods to study the working of enums in Java.