Enum
Enum
A school program needs reliable data and clear rules. An enum defines a fixed set of named instances. Attendance states are clearer as PRESENT and ABSENT than as unexplained numeric codes.
Understand
An enum defines a fixed set of named instances.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
Enums are reference types and may have fields and methods. They are not arbitrary strings that accept any spelling.
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
==andequals()interchangeably—though==is safe for enums. - Use descriptive names and document custom methods properly.
Important boundaries and mistakes
Enums are reference types and may have fields and methods. They are not arbitrary strings that accept any spelling.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Enums are reference types and may have fields and methods.
Java 17 · Main.java · complete practice program
public class Main {
enum Attendance {
PRESENT, ABSENT
}
public static void main(String[] args) {
Attendance state = Attendance.PRESENT;
System.out.println(state);
}
}
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
enum Attendance {
PRESENT, ABSENT
}
public static void main(String[] args) {
Attendance state = Attendance.PRESENT;
System.out.println(state);
}
}
Expected output
PRESENTAn enum defines a fixed set of named instances. Attendance states are clearer as PRESENT and ABSENT than as unexplained numeric codes.
Enums are reference types and may have fields and methods. They are not arbitrary strings that accept any spelling.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Check your understanding
What to remember
An enum defines a fixed set of named instances. Attendance states are clearer as PRESENT and ABSENT than as unexplained numeric codes. Enums are reference types and may have fields and methods. They are not arbitrary strings that accept any spelling.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.