<T> void foo(T bar) {
final Class<? extends T> barType = bar.getClass();
}
This does not compile. barType
is resolved to capture of ? extends Object
. Doesn't make sense to me. Is java type inference so limited or do I miss something here?
<T> void foo(T bar) {
final Class<? extends T> barType = bar.getClass();
}
This does not compile. barType
is resolved to capture of ? extends Object
. Doesn't make sense to me. Is java type inference so limited or do I miss something here?
See the difference in the method definitions below. bar object in method call can get any type of value, so it is of the type <? extends Object>
by default and not <? extends T>
. Basically, T can be anything. So, at compile time it is by default Object type.
<T> void foo(T bar)
{
final Class<? extends Object> barType = bar.getClass();
}
<T> void foo(T bar)
{
final Class<? extends T> barType = bar.getClass();
}