Enums in Java

This little tutorial will help you gather some more knowledge of the benefits and usage of enums and how you can use them in an efficient way.

An Enum in Java has two different modes. The uncompiled version we see in our IDEs and the compiled version for the JVM. For us an Enum is something special, after it is compiled it just a java class with private constructors and some public static members. 

Basic Enum

public enum PlayerStatus {
     ONLINE,
     OFFLINE,
     INGAME,
     AWAY,
     BUSY;
}

Let´s execute a little outline with the enum:

public class Executer {

    public static void main(String[] args) {
        System.out.println(PlayerStatus.AWAY);
    }
    
}

Output

AWAY

Enum with parameter

There are a few business cases in which your enum should have some more attributes. For our PlayerStatus we add the attribute signature (String).
The signature is a text that describes the PlayerStatus.

public enum PlayerStatus {
    ONLINE("I´m online"),
    OFFLINE("My network is gone..."),
    INGAME("Don´t interrupt me"),
    AWAY("Making some things not on the pc.."),
    BUSY("Have to work");

    PlayerStatus(String signature) {
        this.signature = signature;
    }

    private String signature;

   //getter and setter
}

Look at what we have done. Every enumerated value has the constructor with the string parameter now. Furthermore we have added the signature variable and getter and setter.
You can add as much attributes to your enum as you want to.

The constructor of your enum must be placed after(!) the defenitions of the enum values

Let´s execute a little outline with the enum

public class Executer {

    public static void main(String[] args) {
        System.out.println(PlayerStatus.AWAY.getSignature());
    }
    
}

Your output now will be the content of your signature. In my case

Making some things not on the pc..

Equals() or relational operator

How should you compare two enums? Using the == operator or the equals() method?

The answer is very simple.

public static void main(String[] args) {
        PlayerStatus ps1 = PlayerStatus.AWAY;
        PlayerStatus ps2 = PlayerStatus.AWAY;

        if (ps1.equals(ps2)) {
            System.out.println("ps1 equals ps2");
        }
        if (ps1 == ps2) {
            System.out.println("ps1 == ps2");
        }
}

Output:

ps1 equals ps2
ps1 == ps2

Huh! Which one to use now?

I recommend to you the == operator (relational operator). For me this one has two major benefits:

  • no NullPointerException
  • type compatibility check at compile time

Iteration over Enums

You want to get all enum values and their signatures of our PlayerStatus Enum. For example because you want to add them to a DropDownList or something like this.

 public static void main(String[] args) {
        for(PlayerStatus status : PlayerStatus.values()) {
            System.out.println(status);
            System.out.println(status.getSignature());
        }
}

Output

ONLINE
I´m online
OFFLINE
My network is gone...
INGAME
Don´t interrupt me
AWAY
Making some things not on the pc..
BUSY
Have to work

 

So thanks for reading and have fun with Enums.

Feel free to share

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.