Java : Enum with Examples

Enum (Java SE 22 & JDK 22) in Java with Examples.
You will find code samples for most of the Enum<E extends Enum<E>> methods.


Summary

This is the common base class of all Java language enumeration classes. More information about enums, including descriptions of the implicitly declared methods synthesized by the compiler, can be found in section 8.9 of The Java Language Specification.

Class diagram

enum Sample {
    AAA,
    BBB,
    CCC,
}

System.out.println(Sample.AAA.name()); // AAA
System.out.println(Sample.AAA.ordinal()); // 0

System.out.println(Sample.BBB.name()); // BBB
System.out.println(Sample.BBB.ordinal()); // 1

System.out.println(Sample.CCC.name()); // CCC
System.out.println(Sample.CCC.ordinal()); // 2

Enum classes have implicitly declared methods below.

  • public static T valueOf (String)
  • public static T[] values ()
final Sample aaa = Sample.valueOf("AAA");
System.out.println(aaa); // AAA

final Sample[] values = Sample.values();
System.out.println(Arrays.toString(values)); // [AAA, BBB, CCC]

More information about enums, you can be found in section 8.9 of The Java Language Specification.


Constructors

Enum (String name, int ordinal)

Sole constructor.

Programmers can't invoke this constructor.

Methods

protected final Object clone ()

Throws CloneNotSupportedException.

enum Sample {
    AAA, BBB, CCC;

    public void func() {
        System.out.println("func! : " + name());

        try {
            clone();
        } catch (CloneNotSupportedException e) {
            System.out.println("CloneNotSupportedException!");
        }
    }
}

Sample.AAA.func();

// Result
// ↓
//func! : AAA
//CloneNotSupportedException!

final int compareTo (E o)

Compares this enum with the specified object for order.

enum SampleX {
    AAA,
    BBB,
    CCC,
}

enum SampleY {
    CCC,
    BBB,
    AAA,
}

System.out.println(SampleX.AAA.compareTo(SampleX.AAA)); // 0
System.out.println(SampleX.AAA.compareTo(SampleX.BBB)); // -1
System.out.println(SampleX.AAA.compareTo(SampleX.CCC)); // -2

System.out.println(SampleY.AAA.compareTo(SampleY.AAA)); // 0
System.out.println(SampleY.AAA.compareTo(SampleY.BBB)); // 1
System.out.println(SampleY.AAA.compareTo(SampleY.CCC)); // 2

final Optional<Enum.EnumDesc<E>> describeConstable ()

Returns an enum descriptor EnumDesc for this instance, if one can be constructed, or an empty Optional if one cannot be.

enum Sample {
    AAA,
    BBB,
}

final var ret = Sample.AAA.describeConstable();
System.out.println(ret); // Optional[EnumDesc[Sample.AAA]]

final boolean equals (Object other)

Returns true if the specified object is equal to this enum constant.

enum SampleX {
    AAA,
    BBB,
}

enum SampleY {
    AAA,
    BBB,
}

System.out.println(SampleX.AAA.equals(SampleX.AAA)); // true
System.out.println(SampleX.AAA.equals(SampleX.BBB)); // false

System.out.println(SampleY.AAA.equals(SampleY.AAA)); // true
System.out.println(SampleY.AAA.equals(SampleY.BBB)); // false

System.out.println(SampleX.AAA.equals(SampleY.AAA)); // false
System.out.println(SampleY.BBB.equals(SampleX.BBB)); // false

protected final void finalize ()

Deprecated, for removal: This API element is subject to removal in a future version. Finalization has been deprecated for removal.

Deprecated.

final Class<E> getDeclaringClass ()

Returns the Class object corresponding to this enum constant's enum type.

enum Sample {
    AAA,
    BBB,
}

final var decA = Sample.AAA.getDeclaringClass();
System.out.println(decA); // class Sample

final var decB = Sample.BBB.getDeclaringClass();
System.out.println(decB); // class Sample

System.out.println(decA == decB); // true

final int hashCode ()

Returns a hash code for this enum constant.

enum Sample {
    AAA,
    BBB,
    CCC,
}

System.out.println(Sample.AAA.hashCode()); // 648786246
System.out.println(Sample.BBB.hashCode()); // 120360571
System.out.println(Sample.CCC.hashCode()); // 1710814638

final String name ()

Returns the name of this enum constant, exactly as declared in its enum declaration.

The API documentation says "Most programmers should use the toString() method in preference to this one".

enum Sample {
    AAA,
    BBB,
    CCC,
}

System.out.println(Sample.AAA.name()); // AAA
System.out.println(Sample.BBB.name()); // BBB
System.out.println(Sample.CCC.name()); // CCC

final 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).

enum Sample {
    AAA,
    BBB,
    CCC,
}

System.out.println(Sample.AAA.ordinal()); // 0
System.out.println(Sample.BBB.ordinal()); // 1
System.out.println(Sample.CCC.ordinal()); // 2

String toString ()

Returns the name of this enum constant, as contained in the declaration.

enum Sample {
    AAA,
    BBB,
    CCC,
}

final var str1 = Sample.AAA.toString();
System.out.println(str1); // AAA

final var str2 = Sample.BBB.toString();
System.out.println(str2); // BBB

final var str3 = Sample.CCC.toString();
System.out.println(str3); // CCC

static <T extends Enum<T>> T valueOf (Class<T> enumClass, String name)

Returns the enum constant of the specified enum class with the specified name.

enum Sample {
    AAA,
    BBB,
    CCC,
}

final var aaa = Enum.valueOf(Sample.class, "AAA");
System.out.println(aaa); // AAA

final var bbb = Enum.valueOf(Sample.class, "BBB");
System.out.println(bbb); // BBB

try {
    Enum.valueOf(Sample.class, "ZZZ");
} catch (IllegalArgumentException e) {
    System.out.println("IllegalArgumentException! : " + e.getMessage());
}

// Result
// ↓
//IllegalArgumentException! : No enum constant null.ZZZ

Instead, it would be better to use the following method.

  • public static T valueOf (String)
final var aaa = Sample.valueOf("AAA");
System.out.println(aaa); // AAA

Related posts

To top of page