Package java.lang

Class Enum<E extends Enum<E>>

  • All Implemented Interfaces:
    Serializable, Comparable<E>, Modified

    public abstract class Enum<E extends Enum<E>>
    extends Object
    implements Serializable, Comparable<E>, Modified
    The superclass of all enumerated types. Actual enumeration types inherit from this class, but extending this class does not make a class an enumeration type, since the compiler needs to generate special information for it.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Enum​(String name, int ordinal)
      Constructor for constants of enum subtypes.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(E o)
      Compares this object to the specified enum object to determine their relative order.
      boolean equals​(Object other)
      Compares this object with the specified object and indicates if they are equal.
      int hashCode()
      Returns an integer hash code for this object.
      String name()
      Returns the name of this enum constant.
      int ordinal()
      Returns the position of the enum constant in the declaration.
      String toString()
      Returns a string containing a concise, human-readable description of this object.
      static <T extends Enum<T>>
      T
      valueOf​(T[] enumValues, String name)
      Returns the constant with the specified name of the specified enum type.
    • Constructor Detail

      • Enum

        protected Enum​(String name,
                       int ordinal)
        Constructor for constants of enum subtypes.
        Parameters:
        name - the enum constant's declared name.
        ordinal - the enum constant's ordinal, which corresponds to its position in the enum declaration, starting at zero.
    • Method Detail

      • name

        public final String name()
        Returns the name of this enum constant. The name is the field as it appears in the enum declaration.
        Returns:
        the name of this enum constant.
        See Also:
        toString()
      • ordinal

        public final int ordinal()
        Returns the position of the enum constant in the declaration. The first constant has an ordinal value of zero.
        Returns:
        the ordinal value of this enum constant.
      • toString

        public String toString()
        Returns a string containing a concise, human-readable description of this object. In this case, the enum constant's name is returned.
        Overrides:
        toString in class Object
        Returns:
        a printable representation of this object.
      • equals

        public final boolean equals​(Object other)
        Compares this object with the specified object and indicates if they are equal. In order to be equal, object must be identical to this enum constant.
        Overrides:
        equals in class Object
        Parameters:
        other - the object to compare this enum constant with.
        Returns:
        true if the specified object is equal to this Enum; false otherwise.
        See Also:
        Object.hashCode()
      • hashCode

        public final int hashCode()
        Description copied from class: Object
        Returns an integer hash code for this object. By contract, any two objects for which Object.equals(java.lang.Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

        Note that hash values must not change over time unless information used in equals comparisons also changes.

        See Writing a correct hashCode method if you intend implementing your own hashCode method.

        Overrides:
        hashCode in class Object
        Returns:
        this object's hash code.
        See Also:
        Object.equals(java.lang.Object)
      • compareTo

        public final int compareTo​(E o)
        Compares this object to the specified enum object to determine their relative order. This method compares the object's ordinal values, that is, their position in the enum declaration.
        Specified by:
        compareTo in interface Comparable<E extends Enum<E>>
        Parameters:
        o - the enum object to compare this object to.
        Returns:
        a negative value if the ordinal value of this enum constant is less than the ordinal value of o; 0 if the ordinal values of this enum constant and o are equal; a positive value if the ordinal value of this enum constant is greater than the ordinal value of o.
        See Also:
        Comparable
      • valueOf

        public static <T extends Enum<T>> T valueOf​(T[] enumValues,
                                                    String name)
        Returns the constant with the specified name of the specified enum type.
        Type Parameters:
        T - The enum type whose constant is to be returned.
        Parameters:
        enumValues - the class of the enumerated type to search for the constant value.
        name - the name of the constant value to find.
        Returns:
        the enum constant.
        Throws:
        NullPointerException - if either enumType or name are null.
        IllegalArgumentException - if enumType is not an enumerated type or does not have a constant value called name.