0
<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?

ewknmf
  • 9
  • 1

1 Answers1

0

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();
        }
KnockingHeads
  • 1,569
  • 1
  • 22
  • 42