Java Enums (Enumeration)

Last Updated : 8 Jan 2026

In Java, an enum is a special data type used to represent a fixed set of constants. In this chapter, we will learn about the enums (enumeration), how to define and use them.

What is Java Enum?

A Java enum is a data type that is used to define a fixed set of constants. Enum constants are implicitly public, static, and final that means they cannot be changed once defined. According to Java naming conventions, enum constants are written in uppercase letters. Enums were introduced in JDK 1.5 and provide a type-safe way to represent a set of predefined values in your programs.

Java enum (Enumeration) is a data type that is used when we need to represent a fixed set of constants. Unlike C/C++, the enum in Java is more powerful. According to the Java naming conventions, we should have all constants in capital letters. So, we have enum constants in capital letters. The Java enum constants are static and final implicitly. It is available since JDK 1.5.

Common Examples of Enum

The following examples show scenarios where enums are helpful:

  • Days: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
  • Directions: NORTH, SOUTH, EAST, WEST
  • Seasons: SPRING, SUMMER, AUTUMN (or FALL), WINTER
  • Colors: RED, YELLOW, BLUE, GREEN, WHITE, BLACK
  • Sizes: SMALL, MEDIUM, LARGE, EXTRALARGE
  • Planets: MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE
  • Months: JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER

We observe that all enums have fixed values.

Defining enum

Java provides the enum keyword to define the enum data type. We can define an enum either inside the class or outside the class because it is similar to classes. Java enum internally inherits the Enum class, so it cannot inherit any other class, but it can implement many interfaces. We can have fields, constructors, methods, and main() methods in a Java enum.

Syntax

Use the following syntax to define an enum:

Syntax Example

The following shows how to define a simple enum:

The semicolon (;) at the end of the enum constants is optional. For example:

Both the definitions of Java enum are the same. These values inside the braces are called enum constants.

Using an enum as a Variable

We can also create a variable for an enum type. For example,

Here, cloth is a variable of type Size. It has four values.

Access Values of an Enum

You can access the values of an enum using the enum variable or the built-in values() method.

Syntax

Here is the syntax to access enum values:

Example

The following example demonstrates how you can access the the values of an enum:

Output:

Today is: WEDNESDAY
All days of the week:
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY

Defining an Enum Outside the Class

You can define an enum outside a class to make it accessible to multiple classes in the same package.

Example

The following example demonstrates how to define and use an enum outside the class.

Compile and Run

Output:

NORTH
WEST

Defining an enum Inside the Class

You can define an enum inside a class to keep it encapsulated and limit its usage to that class.

Example

The following example demonstrates how to define and use an enum inside a class.

Compile and Run

Output:

WINTER
SPRING
SUMMER
FALL

The main() Method Inside the enum

If we put the main() method inside the enum, we can run the enum directly as we have shown in the following code.

Example

The following example demonstrates how to include and run a main() method inside an enum.

Compile and Run

Output:

WINTER

Using an enum with a switch Statement

You can use enum constants in a switch statement to execute different code based on the enum value.

Example

The following example demonstrates how to use an enum with a switch statement.

Compile and Run

Output:

Monday

Using an Enum as a Variable

You can declare variables of an enum type to store and work with specific enum constants in your program.

Example

The following example demonstrates how to use an enum as a variable.

Compile and Run

Output:

Get ready...

Initializing Specific Values to the enum Constants

As specified earlier, an Enum can have fields, constructors, and methods. The enum constants have an initial value that starts from 0, 1, 2, 3, and so on. But we can initialize the specific value to the enum constants by defining fields and constructors.

Example

The following example demonstrates how to initialize and use specific values with enum constants.

Compile and Run

Output:

WINTER 5
SPRING 10
SUMMER 15
FALL 20

Enum Constructor and Internal Implementation

The constructor of an enum type is always private. If a private constructor is not explicitly declared, the Java compiler automatically creates one. The compiler adds several built-in methods to every enum, including:

  • values(): It returns an array of all enum constants
  • valueOf(String name): It returns the enum constant with the specified name
  • ordinal(): It returns the position of the constant in the enum declaration (starting from 0)

It internally creates a static and final class for the enum.

Example: Enum with Constructor and Values

This example demonstrate the enum with constructor and values in Java.

Internal Code Generated by Compiler

The Java compiler generates code similar to the following for the above enum:

Using an enum in an if Statement

A variable pointing to an enum constant must frequently be compared to other possible constants in the enum type since Java enum are constants.

Syntax

It has the following syntax:

Example

RED, YELLOW, and GREEN are the three constants defined by the TrafficLight enum in this example. The if-else statement checks the signal's current value and then performs the proper action.

Example

Compile and Run

Output:

Get ready...

Java enum Iteration

Java's built-in values() function makes it simple to iterate enums by returning an array containing all of the constants defined in the enum. It enables us to use a conventional for loop or an advanced for loop to iterate through all enum values. When we need to display every option that could be selected, execute operations on every value, or check input against specified constants, enum iteration is useful.

Syntax

It has the following syntax:

Example

All of the days of the week are listed in this example using the Day enum. Day.values() is used by the for loop for looping through and resulting in each enum constant. When working with menus, drop-down menus, or executing operations on particular enum constants without hardcoding them, this method is extremely useful.

Example

Compile and Run

Output:

The Days of the week are:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY

Java Enum Class

An enum class is a special type of class used to define a fixed set of constants. Unlike regular classes, each constant in an enum is automatically created as an instance (object) of the enum class by the compiler. All enum constants are implicitly public, static, and final, so they cannot be changed once defined.

Every enum class implicitly extends the Enum class.

Syntax

It has the following syntax:

Methods of Enum Class

Enum classes have a few predefined methods that are easily accessible. They are as follows.

MethodDescription
clone()It throws CloneNotSupportedException.
compareTo(E o)It compares this enum with the specified object for order.
equals(Object others)It returns true if the specified object is equal to this enum constant.
finalize()Enum classes cannot have finalize methods.
getDeclaringClass()It returns the Class object corresponding to this enum constant's enum type.
hashCode()It returns a hash code for this enum constant.
name()It returns the name of this enum constant, exactly as declared in its enum declaration.
ordinal()It returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).
toString()It returns the name of this enum constant, as contained in the declaration.
valueOf(Class<T> enumType, String name)It returns the enum constant of the specified enum type with the specified name.

Example Using Enum Class Methods

The following example demonstrates creating and using enums using the enum class and its method.

Compile and Run

Output:

The ordinal() values are:
MONDAY -> ordinal: 0
TUESDAY -> ordinal: 1
WEDNESDAY -> ordinal: 2
THURSDAY -> ordinal: 3
FRIDAY -> ordinal: 4
SATURDAY -> ordinal: 5
SUNDAY -> ordinal: 6

 compareTo() example:
MONDAY vs FRIDAY: -4
SUNDAY vs SUNDAY: 0

toString() example:
MONDAY says: Start of the work week...
TUESDAY says: Work in progress...
WEDNESDAY says: Mid of the week...
THURSDAY says: About to Complete...
FRIDAY says: Last day of the work week
SATURDAY says: Weekend!
SUNDAY says: Rest day

name() example:
Enum constant name: THURSDAY

valueOf() example:
Converted from string: FRIDAY

values() iteration:
Start of the work week... (MONDAY)
Work in progress... (TUESDAY)
Mid of the week... (WEDNESDAY)
About to Complete... (THURSDAY)
Last day of the work week (FRIDAY)
Weekend! (SATURDAY)
Rest day (SUNDAY)

Difference Between enum and Enum

The following table shows the difference between enum and Enum in Java.

enumEnum
It is a keyword.It is an abstract class that belongs to java.lang package.
It allows to create of enumerated types.It provides functionality for enum types.
It automatically extends the Enum class.It extends the Object class.
It does not provide any methods.It provides methods like, valueOf(), ordinal(), toString(), name(), etc.

Next TopicJava Annotation