Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Using Reflection API to determine whether class object is an Enum or not

Program to demonstrate how Reflection API can be used to determine whether a class object is an Enum or not in Java

package com.hubberspot.reflection;

enum Days{

 MONDAY,
 TUESDAY,
 WEDNESDAY,
 THURSDAY,
 FRIDAY,
 SATURDAY,
 SUNDAY
}

public class EnumInfo {

   public static void main(String[] args) {
 // Create an Enum 
 Days day = Days.MONDAY;
        // Create a non-Enum Object
 Integer integer = new Integer(1);

 Class enumClass = day.getClass();
 Class integerClass = integer.getClass();

 // isEnum() checks whether object is of type enum or not 
 if(enumClass.isEnum()) {
 // prints the simple name of the class
            System.out.println("Given class " + enumClass.getSimpleName()
    + " is an Enum");
 } else {
     System.out.println("Given class " + enumClass.getSimpleName()
     + " is not an Enum");
 }

 if(integerClass.isEnum()) {
     System.out.println("Given class " + integerClass.getSimpleName()
    + " is an Enum");
 } else {
     System.out.println("Given class " + integerClass.getSimpleName()
    + " is not an Enum");
 }


   }

}




Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot