Java Enum

In Java, an enum is a special class type that represents a collection of constants (unchangeable variables, such as final variables). Java enums are more powerful than simple constants, as they can have fields, methods, and constructors.

An enumeration is created using the enum keyword.

Java Enumerations are Class Types

For example, here is a simple enumeration that lists various apple varieties: // An enumeration of apple varieties. 


enum Apple { Red Delicious, Golden Delicious, Fuji}

An enum type is a reference data type that can be used to define a collection of constants (like MONDAY, TUESDAY, SUNDAY), which improves code readability, maintainability, and type safety.

Characteristics of Enum

1. Enums are type-safe: This means that the compiler actively checks the values, ensuring that only valid ones are assigned.

2. Enums can have fields, methods, and constructors: They’re not just simple constants; Enums can actually hold behaviors and properties, making them quite powerful.

3. Enums can be iterated: You can easily loop through all the values of an enum, which is super convenient!

Syntax:


enum EnumName {
    CONSTANT1, CONSTANT2, CONSTANT3;
}

Explain:

  1. EnumName is the name of the enum.
  2. CONSTANT1, CONSTANT2, CONSTANT3 are the constants.

Example:


// Main.java file
 enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}

public class Main {
    public static void main(String[] args) {
        Day today = Day.TUESDAY;
        System.out.println("Today is: " + today);
    }
}

Explanation:

  1. Day is an enum representing the days of the week.
  2. Day.TUESDAY is used to assign the constant TUESDAY from the Day enum to the variable today.

Output:

Today is: TUESDAY

Enum with values() Method

You can use the values() method to get all the constants of an enum and loop through them.

Example:


// Main.java file
 enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}

public class Main {
    public static void main(String[] args) {
         // Loop through all enum constants
        for (Day day : Day.values()) {
            System.out.println(day);
        }
    }
}

Output:

SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY

Enum with ordinal() Method

The ordinal() method returns the position of the constant


// Main.java file
 enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}

public class Main {
    public static void main(String[] args) {
         // Using ordinal() method to get position
        System.out.println("Position of TUESDAY: " + Day.TUESDAY.ordinal());  // Output: 2
    }
}

Output:

2

Using Enum with a Switch Statement

Using an enum with a switch statement is a common and powerful way to handle different cases based on enum constants. The switch statement allows you to execute different blocks of code depending on the value of the enum.

Example:


// Main.java file
enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}

public class Main {
    public static void main(String[] args) {
        Day today = Day.TUESDAY;  // Set today as TUESDAY

        // Using a switch statement with the enum Day
        switch (today) {
            case SUNDAY:
                System.out.println("It's Sunday, time to relax!");
                break;
            case MONDAY:
                System.out.println("It's Monday, let's start the week strong!");
                break;
            case TUESDAY:
                System.out.println("It's Tuesday, keep going!");
                break;
            case WEDNESDAY:
                System.out.println("It's Wednesday, you're halfway there!");
                break;
            case THURSDAY:
                System.out.println("It's Thursday, almost the weekend!");
                break;
            case FRIDAY:
                System.out.println("It's Friday, time to finish strong!");
                break;
            case SATURDAY:
                System.out.println("It's Saturday, enjoy your day off!");
                break;
            default:
                System.out.println("Invalid day!");
                break;
        }
    }
}

Output:

It’s Tuesday, keep going!

Java Enum – Questions and Answers

Q 1: What is enum in Java?

Ans: Enum is a special data type used to define constant values.

Q 2: Why use enum?

Ans: It improves code readability and type safety.

Q 3: Can enum have methods?

Ans: Yes, enum can have methods and variables.

Q 4: Are enum values final?

Ans: Yes, enum constants are implicitly final.

Q 5: Can enum extend a class?

Ans: No, enums cannot extend classes.

Java Enum – Objective Questions (MCQs)

Q1. Why are enums used in Java?






Q2. How do you define an enum in Java?






Q3. Which method is used to get all values of an enum in Java?






Q4. In Java, which base class is used by all enums by default?






Q5. Is it possible for a Java enum to implement interfaces?






Related Java Enum Topics