Java Reference Library →

enum Keyword Usage and Examples

enum Keyword Usage and Examples

Learn how to use the Java enum keyword with real-world examples. Understand enum declaration, usage in switch-case, enum with constructors and methods, and best practices.

This reference explains enum Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

enum Keyword in Java

In Java, the enum keyword is used to define a set of named constants. Unlike regular constants, enums are more powerful and type-safe. They're ideal when you want to represent a fixed set of values like days of the week, directions, or states in a process.

Why Use enum Instead of Constants?

Java enums offer several advantages over traditional constants (e.g., public static final int):

  • Type safety: You can't assign values outside the enum list.
  • Namespace control: Enum values belong to their enum type.
  • Built-in methods: Enums come with useful methods like values() and valueOf().
  • Extensibility: Enums can have fields, methods, and constructors.

Basic Syntax of enum

enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Using an Enum in a Java Program

You can use enums just like any other class:

public class EnumExample {
    enum Day {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
    }

    public static void main(String[] args) {
        Day today = Day.WEDNESDAY;

        if (today == Day.WEDNESDAY) {
            System.out.println("It's midweek!");
        }
    }
}
It's midweek!

Enum in switch Statement

Enums pair beautifully with switch statements:

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

    public static void main(String[] args) {
        Direction dir = Direction.EAST;

        switch (dir) {
            case NORTH:
                System.out.println("Heading North");
                break;
            case SOUTH:
                System.out.println("Going South");
                break;
            case EAST:
                System.out.println("Turning East");
                break;
            case WEST:
                System.out.println("Moving West");
                break;
        }
    }
}
Turning East

Adding Fields, Constructors, and Methods

Enums aren't just about values. You can attach fields and methods too:

public class EnumAdvanced {
    enum Planet {
        EARTH(5.97), MARS(0.642), JUPITER(1898);

        private double mass; // in 10^24 kg

        Planet(double mass) {
            this.mass = mass;
        }

        public double getMass() {
            return mass;
        }
    }

    public static void main(String[] args) {
        for (Planet p : Planet.values()) {
            System.out.println(p + " has mass " + p.getMass() + " x10^24 kg");
        }
    }
}
EARTH has mass 5.97 x10^24 kg
MARS has mass 0.642 x10^24 kg
JUPITER has mass 1898.0 x10^24 kg

Built-in Methods for Enums

  • values() — Returns an array of all enum constants.
  • valueOf(String) — Converts a string to the corresponding enum constant.
  • ordinal() — Returns the position of the enum constant.
enum Color {
    RED, GREEN, BLUE
}

public class EnumMethods {
    public static void main(String[] args) {
        Color c = Color.valueOf("GREEN");
        System.out.println(c);                  // Output: GREEN
        System.out.println(c.ordinal());        // Output: 1

        for (Color color : Color.values()) {
            System.out.println(color);
        }
    }
}
GREEN
1
RED
GREEN
BLUE

Enum Best Practices

  • Use enums when values are known and fixed.
  • Use enums in switch-case for clean control flow.
  • Attach data and methods to enrich enums beyond basic constants.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →