Class getEnumConstants() method in Java with Examples Last Updated : 27 Dec, 2019 Comments Improve Suggest changes Like Article Like Report The getEnumConstants() method of java.lang.Class class is used to get the Enum constants of this class. The method returns the Enum constants of this class in the form of an array of objects comprising the enum class represented by this class. Syntax: public T[] getEnumConstants() Parameter: This method does not accepts any parameter Return Value: This method returns the specified Enum constants of this class in the form of an array of Enum objects. Below programs demonstrate the getEnumConstants() method. Example 1: Java // Java program to demonstrate // getEnumConstants() method import java.util.*; enum A {} public class Test { public Object obj; public static void main(String[] args) { // returns the Class object for this class Class myClass = A.class; System.out.println("Class represented by myClass: " + myClass.toString()); // Get the Enum constants of myClass // using getEnumConstants() method System.out.println( "Enum constants of myClass: " + Arrays.toString( myClass.getEnumConstants())); } } Output: Class represented by myClass: class A Enum constants of myClass: [] Example 2: Java // Java program to demonstrate // getEnumConstants() method import java.util.*; enum A { RED, GREEN, BLUE; } class Main { private Object obj; public static void main(String[] args) { try { // returns the Class object for this class Class myClass = A.class; System.out.println("Class represented by myClass: " + myClass.toString()); // Get the Enum constants of myClass // using getEnumConstants() method System.out.println( "Enum constants of myClass: " + Arrays.toString( myClass.getEnumConstants())); } catch (Exception e) { System.out.println(e); } } } Output: Class represented by myClass: class A Enum constants of myClass: [RED, GREEN, BLUE] Reference: https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#getEnumConstants-- Comment More infoAdvertise with us Next Article Class getEnumConstants() method in Java with Examples S srinam Follow Improve Article Tags : Java Java-lang package Java-Functions Java.lang.Class Practice Tags : Java Similar Reads Class getConstructors() method in Java with Examples The getConstructors() method of java.lang.Class class is used to get the constructors of this class, which are the constructors that are public. The method returns the constructors of this class in the form of array of Constructor objects. Syntax: public Constructor[] getConstructors() Parameter: Th 2 min read Class getConstructor() method in Java with Examples The getConstructor() method of java.lang.Class class is used to get the specified constructor of this class with the specified parameter type, which is the constructor that is public and its members. The method returns the specified constructor of this class in the form of Constructor object. Syntax 2 min read Class getAnnotations() method in Java with Examples The getAnnotations() method of java.lang.Class class is used to get the annotations present in this class. The method returns an array of annotations present. Syntax: public Annotation[] getAnnotations() Parameter: This method does not accepts any parameter. Return Value: This method returns an arra 2 min read Class getEnclosingClass() method in Java with Examples The getEnclosingClass() method of java.lang.Class class is used to get the enclosing class of this class. The method returns the enclosing class of this class, if this class is a local class or anonymous class declared in that class. Else this method returns null.Syntax: public Class getEnclosingCla 2 min read Class getEnclosingMethod() method in Java with Examples The getEnclosingMethod() method of java.lang.Class class is used to get the enclosing methods of this class. The method returns the enclosing methods of this class if this class is a local class or anonymous class declared in that method. Else this method returns null.Syntax: public Method getEnclos 2 min read Like