A strange error in java generic.

Posted by ??? on Stack Overflow See other posts from Stack Overflow or by ???
Published on 2011-01-16T03:00:09Z Indexed on 2011/01/16 3:53 UTC
Read the original article Hit count: 132

Filed under:
|

This is ok:

Class<? extends String> stringClass = "a".getClass(); 

But this gets error:

<T> void f(T obj) {
    Class<? extends T> objClass = obj.getClass();
}

I know I can cast it like:

<T> void f(T obj) {
    @SuppressWarnings("unchecked")
    Class<? extends T> objClass = (Class<? extends T>) obj.getClass();
}

But why the previous error? Will the next release of Java 7 will support such usage?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics