What Are Enums in Java?
Enums, short for enumerations, are a special data type in Java used to define collections of constants. Instead of declaring a bunch of final static
variables, you can group related constants under an enum
.
Think of an enum as a predefined set of choices that a variable can take—much like a drop-down list in a form.
Why Use Enums?
- Type safety – restricts a variable to only valid values.
- Improves readability and maintainability.
- Enums can have fields, methods, and constructors!
- Great for representing fixed sets like directions, status codes, days, etc.
How to Declare an Enum
enum Direction {
NORTH, SOUTH, EAST, WEST
}
Using Enums in Code
public class EnumDemo {
public static void main(String[] args) {
Direction dir = Direction.NORTH;
System.out.println("The direction is: " + dir);
}
}
The direction is: NORTH
Enum in Switch Statement
Enums work smoothly with switch
statements for better control flow.
public class SwitchEnum {
public static void main(String[] args) {
Direction dir = Direction.SOUTH;
switch (dir) {
case NORTH:
System.out.println("Going up!");
break;
case SOUTH:
System.out.println("Going down!");
break;
case EAST:
System.out.println("Turning right!");
break;
case WEST:
System.out.println("Turning left!");
break;
}
}
}
Going down!
Enums Can Have Fields and Methods
Yes, enums in Java are much more powerful than in many other languages. You can add fields, constructors, and even methods.
enum Planet {
EARTH(5.97), MARS(0.642), JUPITER(1898);
private final double mass; // in 10^24 kg
Planet(double mass) {
this.mass = mass;
}
public double getMass() {
return mass;
}
}
public class EnumWithFields {
public static void main(String[] args) {
for (Planet p : Planet.values()) {
System.out.println(p + " has a mass of " + p.getMass() + " x10^24 kg");
}
}
}
EARTH has a mass of 5.97 x10^24 kg
MARS has a mass of 0.642 x10^24 kg
JUPITER has a mass of 1898.0 x10^24 kg
Enum Methods: values()
and valueOf()
These two methods are available by default:
values()
– returns an array of all enum constants.
valueOf(String name)
– returns the enum constant with the given name.
public class EnumBuiltInMethods {
public static void main(String[] args) {
// List all directions
for (Direction d : Direction.values()) {
System.out.println(d);
}
// Convert string to enum
Direction selected = Direction.valueOf("EAST");
System.out.println("Selected: " + selected);
}
}
NORTH
SOUTH
EAST
WEST
Selected: EAST
Enums with Abstract Methods
Each enum constant can have its own implementation of a method using abstract methods.
enum Operation {
ADD {
public int apply(int a, int b) {
return a + b;
}
},
SUBTRACT {
public int apply(int a, int b) {
return a - b;
}
};
public abstract int apply(int a, int b);
}
public class EnumAbstractMethod {
public static void main(String[] args) {
System.out.println("Addition: " + Operation.ADD.apply(10, 5));
System.out.println("Subtraction: " + Operation.SUBTRACT.apply(10, 5));
}
}
Addition: 15
Subtraction: 5
Best Practices for Enums
- Use enums when you have a fixed set of constants.
- Avoid comparing enum values with
==
and equals()
interchangeably—though ==
is safe for enums.
- Use descriptive names and document custom methods properly.