Java Enums (Enumerations) Tutorial

In this Java tutorial we learn about named integer constants, otherwise known as enums, or enumerations.

We discuss how to declare and access enums, as well as how to use the enum constructor and other enum members.

What is an enum

An enum, or enumeration, is a set of named integer constants. They allow us to create a set of symbolic names that map to known numerical values.

The easiest way to understand an enum is to use a boolean as an example.

A boolean allows us to write the words true or false instead of the numbers 1 or 0. An enum is a custom variation where we declare our own words to map to numbers.

How to declare an enum in Java

We declare an enum by writing the keyword enum , followed by a name and a code block. Inside the code block we specify our name/value pairs of enums, separated by a comma.

Syntax:
enum name {

    element1,
    element2,
    ...
}
Example:
public class Program {
    public static void main(String[] args) {}

    // enums are defined
    // at the same level
    // as methods
    enum DoW {
        Mon,
        Tue,
        Wed,
        Thu,
        Fri,
        Sat,
        Sun
    }
}

In the example above we declared an enum with names for each day of the week. These names represent integers.

Example:
enum DoW {
    Mon, // 0
    Tue, // 1
    Wed, // 2
    Thu, // 3
    Fri, // 4
    Sat, // 5
    Sun  // 6
}

In the example above, each name will represent an integer in our code, like true represents 1 in a boolean.

How to access an enum in Java

To access the value in an enum, we use its name and the element with dot notation.

Syntax:
// declaration
enum name {

    element1,
    element2,
    ...
}

// access
name.element1;
Example:
public class Program {
    public static void main(String[] args) {

        // new enum object
        PSM playerAnim = PSM.idle;
        System.out.println("Player default animation: " + playerAnim);

        // or use it directly
        System.out.println("Current animation: " + PSM.crouching);
    }

    // enum
    enum PSM {
        idle,
        walking,
        running,
        crouching,
        sneaking,
        jumping,
        falling
    }
}

In the example above, we use the enum twice, storing it in an enum object and using it directly.

How to construct an enum in Java

Because an enum is a class, it may have a constructor, as well as other members.

Following is a few notes about enum constructions.

  • The enum elements are always defined at the top, defining anything above them will raise an error.
  • The end of the element list is denoted by a semicolon.
  • The constructor uses the enum name as its name.
  • Each element in the enum may use the constructor.
Example:
public class Program {
    public static void main(String[] args) {

        // new enum object
        DOW currentDayAbbr = DOW.Mon;

        // get full name that was constructed
        String currentDayName = currentDayAbbr.getFullName();

        System.out.println(currentDayName + " (" + currentDayAbbr + ")");
    }

    // enum
    enum DOW {

        // each element in the enum
        // may use the constructor
        Mon("Monday"),
        Tue("Tuesday"),
        Wed("Wednesday"),
        Thu("Thursday"),
        Fri("Friday"),
        Sat("Saturday"),
        Sun("Sunday");

        // property
        private String fullName;

        // the constructor uses
        // the enum name, not any
        // inidivdual element
        private DOW(String fname) {
            this.fullName = fname;
        }

        // getter
        public String getFullName() {
            return fullName;
        }
    }
}

In the example above, we use the constructor to add the full name of each day.

Summary: Points to remember

  • An enum is a set of named constants similar to a boolean and allows us to use words instead of numbers.
  • An enum is an enumeration, not to be confused with an enumerator.
  • Enums are value data types, stored on the stack. They cannot inherit or pass inheritance.