Java Class isEnum() Method



Description

The Java Class isEnum() method returns true if and only if this class was declared as an enum in the source code.

Declaration

Following is the declaration for java.lang.Class.isEnum() method

public boolean isEnum()

Parameters

NA

Return Value

This method returns true if and only if this class was declared as an enum in the source code.

Exception

NA

Getting Enum Status of an Enum Example

The following example shows the usage of java.lang.Class.isEnum() method. In this program, we've created an Enum language and then using getClass() method, the class of the enum is retrieved. Using isEnum(), we've retrieved status as Enum or not and printed it.

package com.tutorialspoint;

// enum showing programming languages
enum Language {
   C, Java;
}

public class ClassDemo {

   public static void main(String args[]) {

      // returns the name and hashCode of this enum constant
      System.out.print("Programming in " + Language.C.toString());      
      System.out.println(", Hashcode = " + Language.C.hashCode()); 
      System.out.print("Programming in " + Language.Java.toString());  
      System.out.println(", Hashcode = " + Language.Java.hashCode());  

      System.out.println(Language.class.isEnum());
   }
} 

Output

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

Programming in C, Hashcode = 2061475679
Programming in Java, Hashcode = 868693306
true

Getting Enum Status of a Class Example

The following example shows the usage of java.lang.Class.isEnum() method. In this program, we've created an instance of ClassDemo and then using getClass() method, the class of the instance is retrieved. Using isEnum(), we've retrieved status as Enum or not and printed it.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String args[]) {


      ClassDemo c = new ClassDemo();
      Class cls = c.getClass();

      System.out.println(cls.isEnum());
   }
} 

Output

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

false
java_lang_class.htm
Advertisements