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()andvalueOf().
- 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 EastAdding 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 kgBuilt-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
BLUEEnum 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.









Comments
Loading comments...