Advertisement

Google Ad Slot: content-top

Java Enum

~1 min read · Java
On this page

In Java, an enum is a special data type that represents a group of constants (unchangeable variables). Enums are used when you have a fixed set of predefined values, such as directions (NORTH, SOUTH, EAST, WEST), days of the week, or months.

Key Points About Enums in Java:

  1. Defined Using enum Keyword:
  • An enum is a distinct data type and is declared using the enum keyword.
  1. Implicit Features:
  • Enums are implicitly static and final.
  • Enum constants are implicitly public, static, and final.
  1. Enum in a Class:
  • Enums can be declared inside or outside a class but not inside a method.
  1. Enum With Methods:
  • Enums can include fields, constructors, and methods.
  1. Enum Cannot Extend Classes:
  • Since enums implicitly extend java.lang.Enum, they cannot extend any other class.


Syntax:

enum EnumName {
    CONSTANT1, CONSTANT2, CONSTANT3;
}

Example 1: Basic Enum

enum Direction { NORTH, SOUTH, EAST, WEST; } public class Main { public static void main(String[] args) { Direction dir = Direction.NORTH; System.out.println("Direction: " + dir); } }
Try it yourself

Example 2: Iterating Through Enums:

You can use the values() method to iterate over all the constants of an enum.

enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; } public class Main { public static void main(String[] args) { for (Day day : Day.values()) { System.out.println(day); } } }
Try it yourself

Example 3: Enum With Fields and Methods:

Enums can have fields, constructors, and methods. Enum constants can also behave like objects.

enum Level { LOW(1), MEDIUM(2), HIGH(3); private int value; // Constructor Level(int value) { this.value = value; } public int getValue() { return value; } } public class Main { public static void main(String[] args) { Level myLevel = Level.HIGH; // Access enum constant and its method System.out.println("Level: " + myLevel); System.out.println("Value: " + myLevel.getValue()); } }
Try it yourself

Example 4: Enum With Switch Case

enum Season { SPRING, SUMMER, FALL, WINTER; } public class Main { public static void main(String[] args) { Season season = Season.SUMMER; switch (season) { case SPRING: System.out.println("Spring is here!"); break; case SUMMER: System.out.println("It's summer time!"); break; case FALL: System.out.println("Leaves are falling."); break; case WINTER: System.out.println("It's cold outside."); break; } } }
Try it yourself

Example 5: Enum With Abstract Methods:

Enums can have abstract methods, and each constant must provide an implementation for it.

enum Operation { ADD { public int apply(int a, int b) { return a + b; } }, SUBTRACT { public int apply(int a, int b) { return a - b; } }, MULTIPLY { public int apply(int a, int b) { return a * b; } }, DIVIDE { public int apply(int a, int b) { return a / b; } }; public abstract int apply(int a, int b); } public class Main { public static void main(String[] args) { int x = 10, y = 5; System.out.println("Addition: " + Operation.ADD.apply(x, y)); System.out.println("Subtraction: " + Operation.SUBTRACT.apply(x, y)); System.out.println("Multiplication: " + Operation.MULTIPLY.apply(x, y)); System.out.println("Division: " + Operation.DIVIDE.apply(x, y)); } }
Try it yourself