I am trying to find the type (Interface or class) of the "Class" instance. I can find isInterface() or isEnum(). But I want to check whether this is a class or not. WHY there is no method like "isClass()"
? any help??
I have this code:
interface A {
}
class B {
}
public class ReflectionDemo {
public static void main(String[] argv) throws Exception {
Class a = A.class;
System.out.println(a.getCanonicalName());
System.out.println(a.getSimpleName());
System.out.println(a.isInterface());
System.out.println(a.isEnum());
Class b = B.class;
System.out.println(b.getCanonicalName());
System.out.println(b.getSimpleName());
System.out.println(b.isInterface());
System.out.println(b.isEnum());
}
}
Misread the question. Given that every type in Java is either a primitive, an interface, a class, or an array, you just need:
System.out.println(!b.isPrimitive() && !b.isInterface() && !b.isArray());
That treats enums as classes, by the way - you could exclude those in the same way, if you want.
See more on this question at Stackoverflow