Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Enums
Enumerations



What Are Enums in Java?

Enums, short for enumerations, are a special data type in Java used to define collections of constants. Instead of declaring a bunch of final static variables, you can group related constants under an enum.

Think of an enum as a predefined set of choices that a variable can take—much like a drop-down list in a form.

Why Use Enums?

How to Declare an Enum

enum Direction {
    NORTH, SOUTH, EAST, WEST
}

Using Enums in Code

public class EnumDemo {
    public static void main(String[] args) {
        Direction dir = Direction.NORTH;
        System.out.println("The direction is: " + dir);
    }
}
The direction is: NORTH

Enum in Switch Statement

Enums work smoothly with switch statements for better control flow.

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

        switch (dir) {
            case NORTH:
                System.out.println("Going up!");
                break;
            case SOUTH:
                System.out.println("Going down!");
                break;
            case EAST:
                System.out.println("Turning right!");
                break;
            case WEST:
                System.out.println("Turning left!");
                break;
        }
    }
}
Going down!

Enums Can Have Fields and Methods

Yes, enums in Java are much more powerful than in many other languages. You can add fields, constructors, and even methods.

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

    private final double mass; // in 10^24 kg

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

    public double getMass() {
        return mass;
    }
}

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

Enum Methods: values() and valueOf()

These two methods are available by default:

public class EnumBuiltInMethods {
    public static void main(String[] args) {
        // List all directions
        for (Direction d : Direction.values()) {
            System.out.println(d);
        }

        // Convert string to enum
        Direction selected = Direction.valueOf("EAST");
        System.out.println("Selected: " + selected);
    }
}
NORTH
SOUTH
EAST
WEST
Selected: EAST

Enums with Abstract Methods

Each enum constant can have its own implementation of a method using abstract methods.

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

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

public class EnumAbstractMethod {
    public static void main(String[] args) {
        System.out.println("Addition: " + Operation.ADD.apply(10, 5));
        System.out.println("Subtraction: " + Operation.SUBTRACT.apply(10, 5));
    }
}
Addition: 15
Subtraction: 5

Best Practices for Enums

QUIZ

Question 1:What is the main purpose of using an enum in Java?

Question 2:Enums in Java can have fields, constructors, and methods.

Question 3:What will be the output of the following code?
enum Direction { NORTH, SOUTH, EAST, WEST }

public class Demo {
    public static void main(String[] args) {
        Direction d = Direction.SOUTH;
        System.out.println(d);
    }
}

Question 4:Which of the following are built-in methods available with enums in Java?

Question 5:Consider the enum with custom constructor and method:
enum Planet {
    EARTH(5.97), MARS(0.642);
    private final double mass;
    Planet(double mass) {
        this.mass = mass;
    }
    public double getMass() {
        return mass;
    }
}
What is the significance of using getMass() here?

Question 6:Enums in Java can be used in switch statements.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M