I noticed something while I was derping around with generics. In the example below, doStuff1
compiles but doStuff2
doesn\'t:
public
You cannot be sure that value
is actually a T, it could also be a subclass of T. getClass()
does return the "real" class of value
, but you can't be sure that's T, therfore it has to read Class extends T>
! This has nothing to do with the fact that T is a type parameter.
[Edit] Ok not quite, i didn't realize method2 doesn't compile. I think this is a compiler issue. I get
Type mismatch: cannot convert from Class to Class extends T>
I think the compiler only realizes that the T object is a Foo. The compiler should know getClass()
returns some Class extends T>
but only seems to know that it returns some Class extends Foo>
. Does that mean Foo is the erasure of the static type
of T?