Search code examples
javaclassbooleanjavadoc

Compile error getting class with getClass()


I'm learning about getClass and how it works.

I read that: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

but i don't really understand why that fails:

boolean b;
Class c = b.getClass();

Can anyone explain to me why it gives me an error?


Solution

  • boolean b; is a primitive datatype and you cannot invoke methods on it using the . operator , Try Boolean b; Boolean is the wrapper class for primitive boolean.

    Try this:

    Boolean b = null;
    Class c = b.getClass();
    

    Or better

    Boolean b = null;
    Class<? extends Boolean>  c = b.getClass();