How do I get constants name of an enum?

To get the constants name of an enumeration you can use the values() method of the enumeration type. This method return an array that contains a list of enumeration constants.

package org.kodejava.basic;

public enum Month {
    JANUARY,
    FEBRUARY,
    MARCH,
    APRIL,
    MAY,
    JUNE,
    JULY,
    AUGUST,
    SEPTEMBER,
    OCTOBER,
    NOVEMBER,
    DECEMBER
}
package org.kodejava.basic;

public class EnumValuesTest {
    public static void main(String[] args) {
        // values() method return an array that contains a list of the
        // enumeration constants.
        Month[] months = Month.values();
        System.out.println("Month size: " + months.length);

        // We can use for each statement to print each enumeration
        // constant.
        for (Month month : Month.values()) {
            System.out.println("Month: " + month);
        }
    }
}

How do I define constructor in enum type?

In the following example you’ll see how to add a constructor to an enum type value. Because an enum is just another class type it can have constructors, fields and methods just like any other classes. Below we define a constructor that accept a string value of color code. Because our enum now have a new constructor declared we have to define the constant named value as RED("FF0000"), ORANGE("FFA500"), etc.

In Java enumeration expanded beyond just as a named constants. Because enum is a class type we can add methods, fields and constructors to the enum type as you can see in the example below.

package org.kodejava.basic;

public enum Rainbow {
    RED("FF0000"),
    ORANGE("FFA500"),
    YELLOW("FFFF00"),
    GREEN("008000"),
    BLUE("0000FF"),
    INDIGO("4B0082"),
    VIOLET("EE82EE");

    private final String colorCode;

    // The constructor of Rainbow enum.
    Rainbow(String colorCode) {
        this.colorCode = colorCode;
    }

    /**
     * Get the hex color code.
     *
     * @return hex color code
     */
    public String getColorCode() {
        return colorCode;
    }
}
package org.kodejava.basic;

public class EnumConstructor {
    public static void main(String[] args) {

        // To get all values of the Rainbow enum we can call the
        // Rainbow.values() method which return an array of
        // Rainbow enum values.
        for (Rainbow color : Rainbow.values()) {
            System.out.println("Color = " + color.getColorCode());
        }
    }
}

How do I use enum in switch statement?

This example show you how to use enumeration or enum type in a switch statement.

package org.kodejava.basic;

public enum RainbowColor {
    RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
}
package org.kodejava.basic;

public class EnumSwitch {
    public static void main(String[] args) {
        RainbowColor color = RainbowColor.INDIGO;

        EnumSwitch es = new EnumSwitch();
        String colorCode = es.getColorCode(color);
        System.out.println("ColorCode = #" + colorCode);
    }

    public String getColorCode(RainbowColor color) {
        String colorCode = "";

        // We use the switch-case statement to get the hex color code of our
        // enum type rainbow colors. We can pass the enum type as expression
        // in the switch. In the case statement we only use the enum named
        // constant excluding its type name.
        switch (color) {
            // We use RED instead of RainbowColor.RED
            case RED -> colorCode = "FF0000";
            case ORANGE -> colorCode = "FFA500";
            case YELLOW -> colorCode = "FFFF00";
            case GREEN -> colorCode = "008000";
            case BLUE -> colorCode = "0000FF";
            case INDIGO -> colorCode = "4B0082";
            case VIOLET -> colorCode = "EE82EE";
        }
        return colorCode;
    }
}

How do I create enumerations type?

Enumeration is a list of named constants. Every most commonly used programming languages support this feature. But in Java it is officially supported since version 5.0. In Java programming language an enumeration defines a class type. Because an enumeration is a class it can have a constructors, methods, and instance variables.

To create an enumeration we use the enum keyword. For example below is a simple enumeration that hold a list of notebook producers:

package org.kodejava.basic;

public enum Producer {
    ACER, APPLE, DELL, FUJITSU, LENOVO, TOSHIBA
}

Below we use our enumeration in a simple program.

package org.kodejava.basic;

public class EnumDeclaration {
    public static void main(String[] args) {
        // Creates an enum variable declaration and assign the value to
        // Producer.APPLE.
        Producer producer = Producer.APPLE;

        if (producer == Producer.LENOVO) {
            System.out.println("Produced by Lenovo.");
        } else {
            System.out.println("Produced by others.");
        }
    }
}

The ACER, APPLE, DELL identifiers are called enumeration constants. Every named constants have an implicitly assigned public and static access modifiers. Although the enumeration is a class type, to create an enumeration variable we don’t use the new keyword. We create an enum just like creating a primitive type of data, as you can see in the example above.