Java.lang.Enum Class in Java
Last Updated :
24 Nov, 2021
Enum class is present in java.lang package. It is the common base class of all Java language enumeration types. For information about enums, refer
enum in java
Class Declaration
public abstract class Enum<E extends Enum>
extends Object
implements Comparable, Serializable
As we can see, that Enum is an
abstract class, so we
can not create object of class Enum.
Methods in Enum Class
Enum class provides 10 useful methods. Most of them are overridden from
Object class. These methods are declared as final in Enum class so the programmer cannot modify any of the enum constants.
- final String name() : This method returns the name of this enum constant, which is exactly as declared in its enum declaration.
Syntax :
public final String name()
Parameters :
NA
Returns :
the name of this enum constant
Java
// Java program to demonstrate name() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.print("Name of enum constant: ");
// name method
System.out.println(c1.name());
}
}
Output:
Name of enum constant: RED
- final int ordinal() : This method returns index of this enumeration constant.
Syntax :
public final int ordinal()
Parameters :
NA
Returns :
the ordinal of this enumeration constant
Java
// Java program to demonstrate ordinal() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args)
{
Color c1 = Color.GREEN;
System.out.print("ordinal of enum constant "+c1.name()+" : ");
// ordinal method
System.out.println(c1.ordinal());
}
}
Output:
ordinal of enum constant GREEN : 1
- String toString() : This method returns a String object representing this enumeration constant. This method is same as name() method.
Syntax :
public String toString()
Parameters :
NA
Returns :
a string representation of this enumeration constant
Overrides :
toString in class Object
Java
// Java program to demonstrate toString() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args)
{
Color c1 = Color.GREEN;
// getting string representation of enum
// using toString() method
String str = c1.toString();
System.out.println(str);
}
}
Output:
GREEN
- final boolean equals(Object obj) : This method returns true if the specified object is equal to this enum constant , otherwise return false.
Syntax :
public final boolean equals(Object obj)
Parameters :
obj - the object too be compared for equality with this enum.
Returns :
true if the specified object is equal to this enum constant.
false otherwise
Overrides :
equals in class Object
Java
// Java program to demonstrate equals() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args)
{
Color c1 = Color.RED;
Color c2 = Color.GREEN;
Color c3 = Color.RED;
// checking equality between enums
// using equals() method
boolean b1 = c1.equals(c2);
boolean b2 = c1.equals(c3);
boolean b3 = c2.equals(null);
System.out.println("is c1 equal to c2 : " + b1);
System.out.println("is c1 equal to c3 : " + b2);
System.out.println("is c2 equal to null : " + b3);
}
}
Output:
is c1 equal to c2 : false
is c1 equal to c3 : true
is c2 equal to null : false
- final int hashCode() : This method returns a hash code for this enum constant. Actually this method contains just one statement, that is "return super.hashCode()" , which in turn calling Object class hashCode() method.
Syntax :
public final int hashCode()
Parameters :
NA
Returns :
a hash code for this enum constant.
Overrides :
hashCode in class Object
Java
// Java program to demonstrate hashCode() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.print("hashcode of enum constant "+ c1.name() +" : ");
// hashcode method
System.out.println(c1.hashCode());
Color c2 = Color.GREEN;
System.out.print("hashcode of enum constant "+ c2.name() +" : ");
// hashcode method
System.out.println(c2.hashCode());
}
}
Output:
hashcode of enum constant RED : 366712642
hashcode of enum constant GREEN : 1829164700
- final int compareTo(E obj) : This method "compares" this enum with the specified object for order. Enum constants are only comparable to other enum constants of the same enum type.
Syntax :
public int compareTo(E obj)
Parameters :
obj - the object to be compared.
Returns :
a negative integer if this object is at less ordinal than the specified object
zero if this object is at equal ordinal than the specified object
a positive integer if this object is at greater ordinal than the specified object
Throws :
NullPointerException - if the argument is null
Java
// Java program to demonstrate compareTo() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args)
{
Color c1 = Color.RED;
Color c2 = Color.GREEN;
Color c3 = Color.RED;
Color c4 = Color.BLUE;
System.out.print("Comparing "+c1.name()+" with "+ c2.name() +" : ");
// compareTo method
System.out.println(c1.compareTo(c2));
System.out.print("Comparing "+c1.name()+" with "+ c3.name() +" : ");
// compareTo method
System.out.println(c1.compareTo(c3));
System.out.print("Comparing "+c4.name()+" with "+ c2.name() +" : ");
// compareTo method
System.out.println(c4.compareTo(c2));
// The following statement throw NullPointerException
// as argument of compareTo method is null
// System.out.println(c4.compareTo(null));
}
}
Output:
Comparing RED with GREEN : -1
Comparing RED with RED : 0
Comparing BLUE with GREEN : 1
- static <T extends Enum> T valueOf(Class enumType,String name) : This method returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type.
Syntax :
public static <T extends Enum> T valueOf(Class enumType,String name)
TypeParameters :
T - The enum type whose constant is to be returned
Parameters :
enumType - the Class object of the enum type from which to return a constant
name - the name of the constant to return
Returns :
the enum constant of the specified enum type with the specified name
Throws :
IllegalArgumentException - if the specified enum type has no
constant with the specified name or the specified class object
does not represent an enum type
NullPointerException - if enumType or name is null
Java
// Java program to demonstrate valueOf() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args)
{
// getting value of enum with specified String
// using valueOf method
Color c1 = Color.valueOf("RED");
Color c2 = Color.valueOf("GREEN");
// name method
System.out.println(c1.name());
System.out.println(c2.name());
// The following statement throw IllegalArgumentException
// as GrEEN is not an enum constant
// Color c3 = Color.valueOf("GrEEN");
// The following statement throw NullPointerException
// as argument of valueOf method is null
// Color c4 = Color.valueOf(null);
}
}
Output:
RED
GREEN
- final Class <E> getDeclaringClass() : This method returns the Class object corresponding to this enum constant's enum type. Any Two enum constants e1 and e2 are of the same enum type if this method returns the same Class object for both.
Syntax :
public final Class <E> getDeclaringClass()
Parameters :
NA
Returns :
the Class object corresponding to this enum constant's enum type
Java
// Java program to demonstrate getDeclaringClass() method
enum Color
{
RED, GREEN, BLUE;
}
enum Day
{
MONDAY, TUESDAY ;
}
public class Test
{
// Driver method
public static void main(String[] args)
{
// getting value of enum with specified String
// using valueOf method
Color c1 = Color.valueOf("RED");
Color c2 = Color.valueOf("GREEN");
Day d1 = Day.valueOf("MONDAY");
Day d2 = Day.valueOf("TUESDAY");
System.out.print("Class corresponding to "+ c1.name() +" : ");
// getDeclaringClass method
System.out.println(c1.getDeclaringClass());
System.out.print("Class corresponding to "+ c2.name() +" : ");
// getDeclaringClass method
System.out.println(c2.getDeclaringClass());
System.out.print("Class corresponding to "+ d1.name() +" : ");
// getDeclaringClass method
System.out.println(d1.getDeclaringClass());
System.out.print("Class corresponding to "+ d2.name() +" : ");
// getDeclaringClass method
System.out.println(d2.getDeclaringClass());
}
}
Output:
Class corresponding to RED : class Color
Class corresponding to GREEN : class Color
Class corresponding to MONDAY : class Day
Class corresponding to TUESDAY : class Day
- final Object clone() : This method guarantees that enums are never cloned, which is necessary to preserve their "singleton" status. It is used internally by compiler to create Enum constants.
Syntax :
public final Object clone() throws CloneNotSupportedException
Parameters :
NA
Returns :
NA
Overrides :
clone in class Object
Throws :
CloneNotSupportedException-if the object's class does not support the Cloneable interface.
Java
// Java program to demonstrate clone() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args)
throws CloneNotSupportedException
{
System.out.println("Enums are never cloned");
Test t = new Test()
{
// final clone method
protected final Object clone()
throws CloneNotSupportedException
{
return new CloneNotSupportedException();
}
};
System.out.println(t.clone());
}
}
Output:
Enums are never cloned
java.lang.CloneNotSupportedException
- final void finalize() : This method guarantees that enum classes cannot have finalize methods.
Syntax :
protected final void finalize()
Parameters :
NA
Returns :
NA
Overrides :
finalize in class Object
Java
// Java program to demonstrate finalize() method
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
// Driver method
public static void main(String[] args) throws Throwable
{
System.out.println("enum classes cannot have finalize methods");
Test t = new Test()
{
// final finalize method
protected final void finalize() throws Throwable
{
// empty implementation
};
};
}
}
Output:
enum classes cannot have finalize methods
Similar Reads
Java.lang.Package Class in Java In Java, the package class was introduced in JDK 1.2 to encapsulate version data associated with a package. As the number of packages increases, knowing the version of the package has become important. This versioning information is retrieved and made available by the ClassLoader instance that loade
9 min read
Java.lang.Class class in Java | Set 2 Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and in
15+ min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
EnumMap Class in Java EnumMap is a specialized implementation of the Map interface for enumeration types. It extends AbstractMap and implements the Map interface in Java. It belongs to java.util package. A few important features of EnumMap are as follows: The EnumMap class is a member of the Java Collections Framework an
12 min read
Java.util.Locale Class in Java | Set 2 Java.util.Locale Class in Java | Set 1 More methods: getDisplayVariant() : java.util.Locale.getDisplayVariant() displays variant of the Locale Syntax : public final String getDisplayVariant() Parameters : ---- Return : ----------- getDisplayVariant(Locale in) : java.util.Locale.Locale in(Locale in)
3 min read
Java.lang package in Java Java.lang package in JavaProvides classes that are fundamental to the design of the Java programming language. The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time. Following are the Important Classes in Java.lan
3 min read
Ennead Class in JavaTuples A Ennead is a Tuple from JavaTuples library that deals with 9 elements. Since this Ennead is a generic class, it can hold any type of value in it. Since Ennead is a Tuple, hence it also has all the characteristics of JavaTuples:Â They are TypesafeThey are ImmutableThey are IterableThey are Serializa
6 min read
Types of Classes in Java A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien
8 min read
Enum with Customized Value in Java Prerequisite : enum in Java By default enums have their own string values, we can also assign some custom values to enums. Consider below example for that. Examples: enum Fruits { APPLE(âREDâ), BANANA(âYELLOWâ), GRAPES(âGREENâ); } In above example we can see that the Fruits enum have three members i
2 min read
EnumSet range() Method in Java The EnumSet.range() method is a part of the java.util package. This method is used to create an EnumSet that contains all enum constants between the specified start and end points, and it should be inclusive. It easily creates a subset of enum constants within a specified range.Syntax of EnumSet ran
2 min read