Enumerations in Java

Java supports enumeration types (in short enum) since Java 5. Enumeration is used to define a list of constants which can be later referenced in source code by its name. Enumeration type in Java is based on concept of the class (as opposed to C and C++ in which it is based on integer type). In short it means that enumeration type is actually a special kind of class and each constant is actually a singleton instance of this class. Such design opens up a lot possibilities that are described below.

Basics of enumeration type

Below is an example of a enumeration type which represents four quarters of the years:

public enum QuarterOfTheYear {
    Q1, Q2, Q3, Q4
}

and its sample usage:

System.out.printf("Quarter1 : %s\n", QuarterOfTheYear.Q1);

for (QuarterOfTheYear quarter : QuarterOfTheYear.values()) {
    System.out.println("Quarter: %s\n", quarter);
}

Available methods

All enums in Java implicitly extend classjava.lang.Enum. Each enumeration type contains following methods:

MethodDescription
Object clone()Throws CloneNotSupportedException (to ensure that there is only one instance of each enum
int compareTo(Enum e)Compares this enum with the specified object for order
boolean equals(Object other)Returns true if the specified object is equal to this enum constant
void finalize()Enum classes cannot have finalize methods
Class<E> getDeclaringClass()Returns the Class object corresponding to this enum constant’s enum type
int hashCode()
Returns a hash code for this enum constant
String name()Returns the name of this enum constant, exactly as declared in its enum declaration
int ordinal()Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero)
String toString()Returns the name of this enum constant, as contained in the declaration
static <T extends Enum<T>>
T valueOf​(Class enumType, String name)
Returns the enum constant of the specified enum type with the specified name
static T valueOf(String name)Returns enum fields with given name
T[] values()Returns table of all enum constants

Most of these methods are already declared in java.lang.Enum class but the last two ones are in fact implicitly declared and synthesized by the compiler.

Comparison

Each enum has equals() method which can be used to check if two enum constants are the same. Given the fact that there can be only one instance of each enum constants, it is also possible and correct to use equal sign == to perform the same check.

Fields and methods in enumeration types

Enumeration type is a Java class so it can contain its own fields, methods and constructors:

public enum QuarterOfTheYear {
    Q1("Quarter One"),
    Q2("Quarter Two"),
    Q3("Quarter Three"),
    Q4("Quarter Four");

    private String description;

    QuarterOfTheYear(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public static QuarterOfTheYear getQuarterByDate(LocalDate date) {
        int quarterNo = date.get(IsoFields.QUARTER_OF_YEAR);
        return QuarterOfTheYear.values()[quarterNo - 1];
    }
}

Differences from standard Java classes

There are however some differences compared to standard Java classes.

The constructor of enum (if defined) must have either private or package-private access. This is to prevent creation of duplicate enum constants. Also it is impossible to call enum constructor yourself.

Additionally, enum cannot extend any other class because it already extends (implicitly) java.lang.Enum class. Yet it can implement interfaces.

About Robert Piasecki

Husband and father, Java software developer, Linux and open-source fan.
This entry was posted in Java, Uncategorized and tagged . Bookmark the permalink.

Leave a comment

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