Enum in Java

|
| By Webner

What is Enum/Enumeration?

Enum/Enumeration is a data type in Java which is used to store a fixed set of constant values. For example, number of days in a week or a set of arithmetic operators (addition, subtraction, multiplication and division) or status of a customer’s bank account (Activated, Deactivated) etc. are static values and can be stored in a variable of Enum type.

Why will someone use Enums?
Before answering this question, let us look into an example where Enum type is replaced with final constant variable.

public class UserStatus {
    public static final int ACTIVE = 1;
    public static final int DEACTIVATED = 2;
    public static final int DELETED = 3;
}
public class User {
    public static void main(String[] args) {
        System.out.println(“Status
            for activated account is: ”+UserStatus.ACTIVE.);
    }
}

Now, in above program, the output will be:-

Status for activated account is: 1
————————————————

Few limitations of using above approach is:

A meaningful message cannot be printed using these variables. Printing ‘UserStatus.ACTIVE’ will give the output as 1 which doesn’t help in understanding the actual status of the user’s account.

The procedure is not type-safe. Any valid integer value can be assigned to these variables by some external procedure which would not serve the purpose as there is no status to represent that value.

And, here’s an example using Enum type variable:

class Sample {
    public enum Operators {
        ADD,
        SUBTRACT,
        MULTIPLY,
        DIVIDE
    }
    public static void main(String[] args) {
        for (Operators op: Operators.values()) { //values() will return an array of all values of enum
            System.out.println(op);
        }
    }
}

Output:
ADD
SUBTRACT
MULTIPLY
DIVIDE
————————-
So, basically, in contrast of conventional ways of using final variables, Enum has several features such as:

It provides type safety to the variable as only a set of predefined values can be stored in that variable.
Enum can be traversed inside a loop easily.
Enum can be used as a singleton class in java which has a fixed set of constant values.
Enum can have constructors, fields and methods and they can easily be used inside a switch.

These are few features for which Enums can be used.

Can Enum be used in Switch statement?
Yes. Another important feature of using Enum in java is that they can be used as an argument in switch statement just like any other primitive data type such as ‘integer’ or ‘char’. Take a look at below example to understand this:

class Sample {
    public enum Operators {
        ADD,
        SUBTRACT,
        MULTIPLY,
        DIVIDE
    }
    public static void main(String[] args) {
        Operators op = Operators.ADD;
        switch (op) {
            case ADD:
                System.out.println(“Add the values.”);
                break;

            case SUBTRACT:
                System.out.println(“Subtract the values.”);
                break;

            case MULTIPLY:
                System.out.println(“Multiply the values.”);
                break;

            case DIVIDE:
                System.out.println(“Divide the values.”);
        }
    }
}

Output of the program will be:

Add the values.
————————
Implementing an interface using Enum

Enum can also implement an interface and override any method like normal class. Below example shows how to implement an interface using Java Enum:

public enum Operators implements Runnable {
    ADD(1), SUBTRACT(2), MULTIPLY(3), DIVIDE(4);
    private int data;
    @Override
    public void run() {
        System.out.println("An example to show that enum can implement interfaces.");
    }
}

So, these are some useful properties, features and capabilities of Enum type that we can use while programthey ming in Java.

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Web development or any other software development assistance please contact us at webdevelopment@webners.com

One comment

  1. Enums are used when we know all possible values at compile time, such as choices on a menu, rounding modes, command line flags, etc.Many thanks for sharing this article.

Leave a Reply

Your email address will not be published. Required fields are marked *