C

Core Java tutorial for beginners

Clean • Professional

Enum in Java – Definition, Features, Methods, and Examples

3 minute

Enum in Java

Enums are one of the most powerful and clean ways to represent fixed sets of constants in Java. They improve readability, remove hard-coded values, and make your code type-safe.


What is an Enum in Java?

An enum (short for enumeration) is a special data type in Java that defines a fixed set of named constants.

It is used when you know all the possible values at compile time.

Example:

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

Enums make your code:

  • Type-safe
  • Cleaner and readable
  • Easy to maintain
  • Free from magic numbers/strings

Why Do We Use Enums?

  • Prevents invalid values
  • Reduces errors caused by strings or integers
  • Supports switch-case
  • Thread-safe by default
  • Can include fields, methods, and constructors
  • More structured than public static final constants

Enum is a Special Type in Java

Internally, every enum:

  • Extends java.lang.Enum
  • Is treated like a final class
  • Cannot extend any other class
  • Can implement interfaces

How Enum Works Internally

When you write:

enum Color { RED, GREEN }

Internally, Java converts it roughly to:

final class Color extends Enum<Color> {
    public static final Color RED = new Color("RED", 0);
    public static final Color GREEN = new Color("GREEN", 1);
}

Each constant becomes a public static final object of the enum type.


Basic Enum Example

enum Color {
    RED, GREEN, BLUE
}

public class Test {
    public static void main(String[] args) {
        Color c = Color.RED;
        System.out.println(c);   // Output: RED
    }
}

Using Enum in Switch-Case

Enums work beautifully with switch-case.

enum Day { MONDAY, FRIDAY, SUNDAY }

switch (Day.FRIDAY) {
    case MONDAY:
        System.out.println("Start of week");
        break;
    case FRIDAY:
        System.out.println("Weekend is coming!");
        break;
    case SUNDAY:
        System.out.println("Weekend!");
        break;
}

Built-in Enum Methods

Java provides useful methods for all enums:

learn code with durgesh images

MethodDescription
values()Returns all enum constants
valueOf(String name)Returns constant with given name
ordinal()Returns index (0-based)
name()Returns constant name

Example:

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

Enums with Fields, Constructors, and Methods

Enums can behave like classes.

Example: Enum with Field and Constructor

enum Status {
    SUCCESS(200),
    ERROR(500),
    NOT_FOUND(404);

    private int code;

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

    public int getCode() {
        return code;
    }
}

Usage:

System.out.println(Status.SUCCESS.getCode());  // Output: 200

Enums Can Have Custom Methods

enum Level {
    LOW {
        @Override
        public String message() {
            return "Low Level";
        }
    },
    HIGH {
        @Override
        public String message() {
            return "High Level";
        }
    };

    public abstract String message();
}

Enums Can Implement Interfaces

interface Printable {
    void print();
}

enum Color implements Printable {
    RED, GREEN;

    public void print() {
        System.out.println("Color: " + this.name());
    }
}

Enum Best Practices

  • Use UPPERCASE for enum constants
  • Use enums instead of static constants
  • Don't overuse fields unless necessary
  • Use EnumSet and EnumMap for performance
  • Use switch-case for clean conditional logic

Real-World Uses of Enum

  • Days of the week
  • Order status
  • HTTP response codes
  • Directions
  • Game levels
  • Payment status
  • Logging levels

Example:

enum OrderStatus {
    PENDING, PROCESSING, SHIPPED, DELIVERED, CANCELLED
}


Article 0 of 0