Since the advent of generics in Java 5, the java.lang.Class class has been common to the type it represents. This allows you to create an object through their Class without actuation and allows you to use other useful idioms.
This is why the compiler may become too smart and see that it is able to fully evaluate the expression at compile time: your expression is always false . The compiler gives you a type compatibility error, but this is a direct result of the fact that the expression is a compile-time constant.
One way to avoid this is to use java.lang.Object as a type. Once you do this, the compiler will no longer be able to accurately evaluate the result type of getType , thereby avoiding the error.
Another way to fix this problem is to cast the return type into a non-generic Class
System.out.println(((Class)b.getClass())==parent.class);
or using an intermediate variable of a non-general type:
Class bClass = b.getClass(); System.out.println(bClass==parent.class);
source share