Enumerated Types and enum Keyword

Java introduced the enum keyword to define a set of named constants. Enumerated types provide a way to define a collection of constant values that can be referred to by name. Enums are a powerful feature in Java, allowing for type-safe handling of fixed sets of constants.

What is an Enum?

An enum (short for enumeration) is a special Java type used to define collections of constants. Each constant in an enum is a unique instance of that enum type. For example, the days of the week can be defined as an enum:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}Code language: PHP (php)

In the above code, Day is an enum type, and SUNDAY, MONDAY, etc., are its constants. These constants are implicitly public, static, and final.

Why Use Enums?

Enums provide several advantages:

  • Type Safety: Enums are type-safe, meaning they prevent unexpected values.
  • Readability: Code is more readable as the constants are well-defined and self-explanatory.
  • Maintainability: Easy to update and manage the set of constants.
  • Integration with Switch Statements: Enums can be used with switch statements.
  • Predefined Methods: Enums provide built-in methods like values() and ordinal().

Declaring an Enum with Constructor and Methods

Enums can include constructors, fields, and methods. Here’s an example:

public enum AccountType {
    SAVINGS("Savings Account"),
    CURRENT("Current Account"),
    FIXED_DEPOSIT("Fixed Deposit Account");

    private String description;

    // Constructor
    AccountType(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}Code language: JavaScript (javascript)

In this example, each enum constant (SAVINGS, CURRENT, FIXED_DEPOSIT) is associated with a description through the constructor.

Accessing Enum Constants

Enum constants can be accessed using the dot operator:

AccountType account = AccountType.SAVINGS;
System.out.println(account);  // Output: SAVINGS
System.out.println(account.getDescription());  // Output: Savings AccountCode language: JavaScript (javascript)

Using Enums in Switch Statements

Enums can be effectively used in switch statements, enhancing the clarity of the code:

AccountType accountType = AccountType.CURRENT;

switch (accountType) {
    case SAVINGS:
        System.out.println("This is a savings account.");
        break;
    case CURRENT:
        System.out.println("This is a current account.");
        break;
    case FIXED_DEPOSIT:
        System.out.println("This is a fixed deposit account.");
        break;
}Code language: JavaScript (javascript)

Example

Here’s a simple enum program for different types of bank accounts in Lotus Bank

public class BankAccountTypes {

    enum AccountType {
        SAVINGS, CURRENT, FIXED_DEPOSIT;
    }

    public static void main(String[] args) {
        System.out.println("Account Types in Lotus Bank:");
        for (AccountType type : AccountType.values()) {
            System.out.println(type);
        }

        // Accessing specific enum constants
        AccountType selectedAccount = AccountType.SAVINGS;
        System.out.println("Selected Account: " + selectedAccount);

        // Using ordinal()
        System.out.println("Ordinal of FIXED_DEPOSIT: " + AccountType.FIXED_DEPOSIT.ordinal());

        // Using name()
        System.out.println("Name of CURRENT: " + AccountType.CURRENT.name());
    }
}
/*
Account Types in Lotus Bank:
SAVINGS
CURRENT
FIXED_DEPOSIT
Selected Account: SAVINGS
Ordinal of FIXED_DEPOSIT: 2
Name of CURRENT: CURRENT
*/

Enumerated types and the enum keyword in Java provide a structured and type-safe way to define a set of constants. They enhance code readability, maintainability, and clarity by allowing developers to handle constant values with descriptive names. Enums can be simple or complex, supporting constructors, fields, methods, and even abstract methods, making them a versatile feature in Java.

Scroll to Top