Enum in Java
Introduction
An Enum (Enumeration) in Java is a special data type used to define a fixed set of constants.
For example, days of the week, months, directions, or status values can be represented using an enum.
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
Why Use Enum?
Enum is useful when a variable should contain only a fixed set of values.
Examples:
- Days of the week
- Months
- Directions
- Traffic light colors
- User roles
- Order status
Enum Syntax
enum EnumName { CONSTANT1, CONSTANT2, CONSTANT3 }
Example:
enum Color { RED, GREEN, BLUE }
Basic Enum Example
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } public class EnumExample { public static void main(String[] args) { Day today = Day.MONDAY; System.out.println(today); } }
Output
MONDAY
Enum with Switch
Enum can be used with a switch statement.
enum Day { MONDAY, TUESDAY, WEDNESDAY } public class EnumSwitch { public static void main(String[] args) { Day day = Day.TUESDAY; switch (day) { case MONDAY: System.out.println("Start of the week"); break; case TUESDAY: System.out.println("Today is Tuesday"); break; case WEDNESDAY: System.out.println("Today is Wednesday"); break; } } }
Output
Today is Tuesday
Enum with values()
The values() method returns all constants of an enum.
enum Color { RED, GREEN, BLUE } public class EnumValues { public static void main(String[] args) { for (Color color : Color.values()) { System.out.println(color); } } }
Output
RED GREEN BLUE
Enum with valueOf()
The valueOf() method converts a String into the corresponding enum constant.
enum Color { RED, GREEN, BLUE } public class EnumValueOf { public static void main(String[] args) { Color color = Color.valueOf("GREEN"); System.out.println(color); } }
Output
GREEN
The string must exactly match an enum constant.
Enum with ordinal()
The ordinal() method returns the position of an enum constant.
The first constant has index 0.
enum Color { RED, GREEN, BLUE } public class EnumOrdinal { public static void main(String[] args) { System.out.println(Color.RED.ordinal()); System.out.println(Color.GREEN.ordinal()); System.out.println(Color.BLUE.ordinal()); } }
Output
0 1 2
Enum with Constructor and Method
An enum can also have fields, constructors, and methods.
enum Level { LOW(1), MEDIUM(2), HIGH(3); private int value; Level(int value) { this.value = value; } public int getValue() { return value; } } public class EnumConstructor { public static void main(String[] args) { System.out.println(Level.LOW.getValue()); System.out.println(Level.HIGH.getValue()); } }
Output
1 3
Enum vs Constant Variables
| Enum | Constant Variables |
|---|---|
| Defines a fixed set of related values | Usually defines individual constants |
| Provides type safety | Less type-safe |
| Can have methods and constructors | Normally used for simple constant values |
Easy to use with switch | Can be used with switch depending on type |
| Represents a specific set of choices | Represents individual constant values |
Real-Life Example
Consider an online order system:
enum OrderStatus { PENDING, CONFIRMED, SHIPPED, DELIVERED, CANCELLED }
An order can have one of these predefined statuses.
OrderStatus status = OrderStatus.SHIPPED;
This prevents unrelated values from being used as the order status.
Advantages of Enum
- Provides type safety.
- Represents a fixed set of constants.
- Improves code readability.
- Easy to use with
switch. - Can contain fields, constructors, and methods.
- Helps prevent invalid values.
Important Points
enumis a special Java type.- It is used to define fixed constants.
- Enum constants are written in uppercase by convention.
values()returns all enum constants.valueOf()returns an enum constant from a matching String.ordinal()returns the position of an enum constant.- An enum can have constructors, fields, and methods.
- Enum is useful when only a predefined set of values is allowed.
Conclusion
An Enum is useful in Java when a variable should contain only a fixed set of related values. It improves readability and type safety and is commonly used for days, months, statuses, directions, roles, and other predefined choices.