Sometimes a variable should only be allowed to have a small, fixed set of values.
For example, imagine we are creating an application where an order can have only these statuses:
PENDING
SHIPPED
DELIVERED
CANCELLEDWe could store the status as a String, but then someone could accidentally write:
String status = "SomethingElse";There is nothing stopping us from using an invalid value.
Java provides enum for situations like this.
An enum is a special type used to define a fixed set of named constants.
Creating an Enum
We create an enum using the enum keyword.
enum OrderStatus {
PENDING,
SHIPPED,
DELIVERED,
CANCELLED
}Here, OrderStatus is an enum, and its allowed values are:
PENDING
SHIPPED
DELIVERED
CANCELLEDWe can now use it as a type:
OrderStatus status = OrderStatus.PENDING;This is much safer than using a normal String because status can only contain one of the values defined in the enum.
Using an Enum
Let's create a simple example:
enum OrderStatus {
PENDING,
SHIPPED,
DELIVERED,
CANCELLED
}
class Main {
public static void main(String[] args) {
OrderStatus status = OrderStatus.SHIPPED;
System.out.println(status);
}
}Output:
SHIPPEDNotice how we access an enum value using the enum name:
OrderStatus.SHIPPEDWhy Use Enum Instead of String?
Suppose we use a String:
String status = "SHIPPED";Someone could accidentally write:
String status = "Shipped";or:
String status = "SHIPED";Java won't consider these invalid at compile time because they are still valid Strings.
With an enum:
OrderStatus status = OrderStatus.SHIPPED;Java knows exactly which values are allowed.
This gives us better type safety and makes our code easier to understand.
Enum with switch
Enums work very well with switch statements.
For example:
enum OrderStatus {
PENDING,
SHIPPED,
DELIVERED,
CANCELLED
}
class Main {
public static void main(String[] args) {
OrderStatus status = OrderStatus.SHIPPED;
switch (status) {
case PENDING:
System.out.println("Order is pending.");
break;
case SHIPPED:
System.out.println("Order has been shipped.");
break;
case DELIVERED:
System.out.println("Order has been delivered.");
break;
case CANCELLED:
System.out.println("Order was cancelled.");
break;
}
}
}Output:
Order has been shipped.This is a very common use of enums.
Getting All Enum Values
We can use the values() method to get all the values defined in an enum.
For example:
enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY
}We can loop through all the values:
for (Day day : Day.values()) {
System.out.println(day);
}Output:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAYThis is useful when we need to work with every possible enum value.
Converting a String to an Enum
Sometimes we receive a String from a user or an external system and need to convert it into an enum.
We can use valueOf():
OrderStatus status = OrderStatus.valueOf("SHIPPED");
System.out.println(status);Output:
SHIPPEDThe String must match the enum constant exactly.
For example:
OrderStatus.valueOf("shipped");would cause an exception because "shipped" is different from "SHIPPED".
Enum Can Have Methods
An enum can contain methods and other members, so it can be more powerful than just a list of constants.
For example:
enum OrderStatus {
PENDING,
SHIPPED,
DELIVERED,
CANCELLED;
void showMessage() {
System.out.println("Current status: " + this);
}
}Now we can write:
OrderStatus status = OrderStatus.SHIPPED;
status.showMessage();Output:
Current status: SHIPPEDHere, every enum constant can use the showMessage() method.
Enum with Additional Data
Enums can also have fields and constructors.
For example, suppose each order status has a description:
enum OrderStatus {
PENDING("Order is waiting."),
SHIPPED("Order has been shipped."),
DELIVERED("Order has been delivered.");
private String message;
OrderStatus(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}Now:
OrderStatus status = OrderStatus.SHIPPED;
System.out.println(status.getMessage());Output:
Order has been shipped.This is useful when each enum value needs some additional information.
Enum vs Constants
You might wonder why we need enums when Java already has final constants.
For example:
static final String PENDING = "PENDING";
static final String SHIPPED = "SHIPPED";
static final String DELIVERED = "DELIVERED";This works, but a String variable can still contain any String.
An enum creates a proper type:
OrderStatus status = OrderStatus.SHIPPED;Now Java knows that status must be an OrderStatus.
This makes enums a better choice when you have a fixed group of related values.
Common Uses of Enum
Enums are commonly used for things such as:
Order status
User roles
Days of the week
Months
Directions
Payment types
Application states
Difficulty levelsFor example:
enum Difficulty {
EASY,
MEDIUM,
HARD
}Then:
Difficulty level = Difficulty.HARD;The main idea is simple:
Use an enum when a variable should have one value from a fixed set of related options.
For example, if an order can only be PENDING, SHIPPED, DELIVERED, or CANCELLED, an enum is a clean and type-safe way to represent those possible states.