Java.lang Package Classes

Java Enum - getDeclaringClass() Method



The java.lang.Enum.getDeclaringClass() method returns the Class object corresponding to this enum constant's enum type. Two enum constants e1 and e2 are of the same enum type if and only if e1.getDeclaringClass() == e2.getDeclaringClass().

Syntax

public final Class<E> getDeclaringClass()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

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

Exception

NA.

Example:

In the example below, the java.lang.Enum.getDeclaringClass() method returns the given Class object corresponding to the given enum constant's enum type.

import java.lang.*;

public class MyClass {
  
  //creating an enum
  public enum WeekDay{
    MON, TUE, WED, THU, FRI;
  }

  public static void main(String[] args) {
    
  //returns the class object corresponding to the 
  //enum constant's enum type  
  System.out.println(WeekDay.MON.getDeclaringClass());  
  }
}

The output of the above code will be:

class MyClass$WeekDay

❮ Java.lang - Enum