Pin It

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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot