VOOZH about

URL: https://www.geeksforgeeks.org/java/enum-in-java/

⇱ enum in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

enum in Java

Last Updated : 27 May, 2026

In Java, enum is a special data type used to define a fixed set of constant values in a type-safe way. It improves code clarity by replacing numeric or string constants with meaningful names. Enums are commonly used when a variable can have only a limited set of predefined values.

  • Provides type safety by restricting values to predefined constants
  • Improves code readability and maintainability
  • Commonly used for fixed data like days, directions, and states

Output
RED

Declaration of enum in Java

Enum declaration can be done outside a class or inside a class but not inside a method.

enum EnumName {

CONSTANT1, CONSTANT2, CONSTANT3;

}

Declaration outside the class

As we have seen in the above example, enums can be declared outside a class and accessed directly

Declaration inside a class

Enums can also be declared inside a class but not inside a method.


Output
RED

Properties of Enum in Java

There are certain properties followed by Enum as mentioned below:

  • Class Type: Internally implemented as a class.
  • Enum Constants: Each constant is an object of the enum type.
  • Switch Support: Can be used in switch statements.
  • Implicit Modifiers: Constants are public static final.
  • Inheritance: Enums cannot extend classes but can implement interfaces.

Enum in a Switch Statement

Enums can be used in switch statements to handle different cases based on the enum constants.


Output
Other color

Enum with Methods and Constructor

Enums can have constructors and methods, executed separately for each constant


Output
Constructor called for: RED
Constructor called for: GREEN
Constructor called for: BLUE
Color is: RED

Enum with Abstract Methods

Enums can declare abstract methods that each constant must implement.


Output
1st day
5th day

Iterating Enums Using values()

Use EnumType.values() to loop through all enum constants.


Output
RED
GREEN
BLUE

Enum and EnumSet (Specific Range Iteration)

EnumSet.range() allows iteration over a specific range of enum constants.


Output
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY

Creating a Class with an Enum Member

We can combine enums with regular classes to organize your program logic. An enum can be a member variable in a class, and methods can perform actions based on the enum value.


Output
Mondays are tough
Wednesday are okay
Fridays are exciting
Saturdays are relaxing
Sunday are for rest

Explanation:

  • The EnumTest class in above code is created with member of type Day. It has constructor which takes Day enum as an argument and assigns it.
  • The class has method tellItLikeItIs(), which prints message based on value of day.
  • The main method includes objects of EnumTest using different Day enum values and calling tellItLikeItIs() method on each.

NOTE: The new keyword is used because EnumTest is a regular class, not an enum, so we create instances and pass the enum value to its constructor.

Comment
Article Tags:
Article Tags: