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 EnumThe following examples show scenarios where enums are helpful:
We observe that all enums have fixed values. Defining enumJava 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. SyntaxUse the following syntax to define an enum: Syntax ExampleThe 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 VariableWe 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 EnumYou can access the values of an enum using the enum variable or the built-in values() method. SyntaxHere is the syntax to access enum values: ExampleThe 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 ClassYou can define an enum outside a class to make it accessible to multiple classes in the same package. ExampleThe following example demonstrates how to define and use an enum outside the class. Output: NORTH WEST Defining an enum Inside the ClassYou can define an enum inside a class to keep it encapsulated and limit its usage to that class. ExampleThe following example demonstrates how to define and use an enum inside a class. Output: WINTER SPRING SUMMER FALL The main() Method Inside the enumIf we put the main() method inside the enum, we can run the enum directly as we have shown in the following code. ExampleThe following example demonstrates how to include and run a main() method inside an enum. Output: WINTER Using an enum with a switch StatementYou can use enum constants in a switch statement to execute different code based on the enum value. ExampleThe following example demonstrates how to use an enum with a switch statement. Output: Monday Using an Enum as a VariableYou can declare variables of an enum type to store and work with specific enum constants in your program. ExampleThe following example demonstrates how to use an enum as a variable. Output: Get ready... Initializing Specific Values to the enum ConstantsAs 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. ExampleThe following example demonstrates how to initialize and use specific values with enum constants. Output: WINTER 5 SPRING 10 SUMMER 15 FALL 20 Enum Constructor and Internal ImplementationThe 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:
It internally creates a static and final class for the enum. Example: Enum with Constructor and ValuesThis example demonstrate the enum with constructor and values in Java. Internal Code Generated by CompilerThe Java compiler generates code similar to the following for the above enum: Using an enum in an if StatementA variable pointing to an enum constant must frequently be compared to other possible constants in the enum type since Java enum are constants. SyntaxIt has the following syntax: ExampleRED, 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. ExampleCompile and RunOutput: Get ready... Java enum IterationJava'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. SyntaxIt has the following syntax: ExampleAll 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. ExampleCompile and RunOutput: The Days of the week are: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY Java Enum ClassAn 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. SyntaxIt has the following syntax: Methods of Enum ClassEnum classes have a few predefined methods that are easily accessible. They are as follows.
Example Using Enum Class MethodsThe following example demonstrates creating and using enums using the enum class and its method. 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 EnumThe following table shows the difference between enum and Enum in Java.
Next TopicJava Annotation |
We request you to subscribe our newsletter for upcoming updates.