Program to demonstrate how Reflection API can be used to determine whether a class object is an Enum or not in Java
Output of the program :
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 :