The following example shows how to verify whether the given class is an interface, enum or class.
Class provides the following APIs,
public boolean isInterface()
- Returns true if the class object is an interface otherwise false.
public boolean isEnum()
- Returns true if the class object is an enum type otherwise false.
private static void checkClassOrInterface(Class<?> clazz) { if (clazz.isInterface()) {
System.out.println(clazz + " is an Interface.");
} else if (clazz.isEnum()) {
System.out.println(clazz + " is an Enum.");
} else {
System.out.println(clazz + " is a Class.");
}
}
}
It gives the following output,
class java.lang.String is a Class.
class java.util.Stack is a Class.
interface java.lang.Runnable is an Interface.
interface java.io.Serializable is an Interface.
class java.lang.Thread$State is an Enum.
class com.bethecoder.tutorials.reflection.GetClassType$ServerStatus is an Enum.