Java.lang.Enum.getDeclaringClass() Method

Advertisements


Description

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

Declaration

Following is the declaration for java.lang.Enum.getDeclaringClass() method

public final Class<E> getDeclaringClass()

Parameters

  • NA

Return Value

This method returns the Class object corresponding to this enum constant's enum type.

Exception

  • NA

Example

The following example shows the usage of java.lang.Enum.getDeclaringClass() method.

package com.tutorialspoint;

import java.lang.*;

enum newEnum {
 
  M {     
    },

  N {
    };
}

public class EnumDemo {

   public static void main(String args[]) {
       
     /* returns the Class object corresponding to this enum
     constant's enum type */
     System.out.println(newEnum.M.getDeclaringClass());  
   }
} 

Let us compile and run the above program, this will produce the following result:

class newEnum



Advertisements
Advertisements