- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java





- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java


Java Enums
Enumerations
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.
QUIZ
Question 1:What is the main purpose of using an enum
in Java?
Question 2:Enums in Java can have fields, constructors, and methods.
Question 3:What will be the output of the following code?
enum Direction { NORTH, SOUTH, EAST, WEST }
public class Demo {
public static void main(String[] args) {
Direction d = Direction.SOUTH;
System.out.println(d);
}
}
enum Direction { NORTH, SOUTH, EAST, WEST }
public class Demo {
public static void main(String[] args) {
Direction d = Direction.SOUTH;
System.out.println(d);
}
}
Question 4:Which of the following are built-in methods available with enums in Java?
Question 5:Consider the enum with custom constructor and method:
enum Planet {
EARTH(5.97), MARS(0.642);
private final double mass;
Planet(double mass) {
this.mass = mass;
}
public double getMass() {
return mass;
}
}
What is the significance of using getMass()
here?
enum Planet {
EARTH(5.97), MARS(0.642);
private final double mass;
Planet(double mass) {
this.mass = mass;
}
public double getMass() {
return mass;
}
}