Visit Sponsor

Written by 12:57 pm Core Java

Java Enum Tutorial – Complete Guide and Examples

Enums (enumerations) in Java are a type-safe way to define a fixed set of constants. They improve code clarity, reduce errors, and enable powerful features like methods, constructors, interfaces, and switch support. This tutorial covers everything you need to work with enums confidently in modern Java.

Overview

Java enum was introduced in Java 5 as a language-level construct. Unlike constants defined using static final fields, enums provide:

  • Strong type safety
  • Namespace grouping
  • Ability to define behavior
  • Integration with switch statements
  • Support for interfaces and methods

Enums are ideal when a variable can hold only a limited set of predefined values.

Defining a Basic Enum

You define an enum using the enum keyword.

public enum Direction {
    NORTH,
    SOUTH,
    EAST,
    WEST
}

This creates a new type named Direction with four possible values.

Using Enum Values

Use enum values directly:

Direction dir = Direction.NORTH;

if (dir == Direction.SOUTH) {
    System.out.println("Heading south");
}

Comparison using == is safe because enums are singletons.

Enum in Switch Statement

Enums work naturally with switch:

switch (dir) {
    case NORTH:
        System.out.println("North selected");
        break;
    case EAST:
        System.out.println("East selected");
        break;
    default:
        System.out.println("Other direction");
}

Switch with enums improves readability over strings or integers.

Enum as a Full Class

Java enums can have fields, constructors, and methods.

public enum Status {
    STARTED(1),
    PROCESSING(2),
    COMPLETED(3);

    private final int code;

    private Status(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }
}

Each constant calls the enum constructor with a specific code.

Using Enum Methods

Status status = Status.PROCESSING;
System.out.println("Code: " + status.getCode());

This prints the associated code, demonstrating behavior encapsulated within enum types.

Built-In Enum Methods

Java enums provide useful built-in methods:

  • values() – returns all constants
  • valueOf(String name) – returns enum constant by name
  • name() – returns identifier string
  • ordinal() – position of constant

Example:

for (Direction d : Direction.values()) {
    System.out.println(d + " ordinal " + d.ordinal());
}

Overriding toString()

You can override toString() for custom string representation:

public enum Fruit {
    APPLE,
    ORANGE,
    BANANA;

    @Override
    public String toString() {
        return "Fruit: " + name();
    }
}

This returns a formatted string when the enum is printed.

Enums with Abstract Methods

Enums can declare abstract methods that each constant implements:

public enum Operation {
    ADD {
        public int apply(int a, int b) { return a + b; }
    },
    MULTIPLY {
        public int apply(int a, int b) { return a * b; }
    };

    public abstract int apply(int a, int b);
}

Usage:

int result = Operation.ADD.apply(5, 3);

This pattern enables behavior specific to each constant.

Enums Implementing Interfaces

Enums can implement interfaces for flexibility:

public interface Describable {
    String getDescription();
}

public enum Planet implements Describable {
    EARTH("Third planet"),
    MARS("Red planet");

    private final String description;

    private Planet(String description) {
        this.description = description;
    }

    @Override
    public String getDescription() {
        return description;
    }
}

Using an interface makes enum values usable in polymorphic code.

EnumSet and EnumMap

For performance and clarity when working with enums as keys or sets:

EnumSet

EnumSet<Direction> directions = EnumSet.of(Direction.NORTH, Direction.SOUTH);

EnumSet is more efficient than general Set implementations.

EnumMap

EnumMap<Status, String> map = new EnumMap<>(Status.class);
map.put(Status.STARTED, "Job started");

EnumMap uses enum constants internally as array indices for performance.

Serialization and Enums

Java enums are inherently serializable and guaranteed to preserve singleton behavior during deserialization, unlike regular classes.

Common Use Cases

Use enums in cases such as:

  • State machines (e.g., order states)
  • Fixed option sets (e.g., menu types)
  • Switchable behavior (e.g., command patterns)
  • Mapping codes to values

Enums provide compile-time safety and maintainability over string/constant patterns

Best Practices (Senior Engineering Insight)

  • Prefer enums over static final constants when the set of values is fixed
  • Use constructors to define associated data
  • Keep enum behavior cohesive (don’t mix unrelated logic)
  • Use EnumSet and EnumMap for collections involving enums

These practices reduce bugs and improve code readability

Summary

Java enums are powerful, type-safe constructs that go beyond simple constant lists. They can contain fields, methods, and logic, making them suitable for modeling fixed sets of behaviors or states. With switch support, polymorphism, and built-in methods, enums are an essential part of modern Java programming.

Visited 7 times, 1 visit(s) today
Close