2
public abstract class A<T> {
    public static void test(A i) { for (String s : i.get()) {} }
    public abstract Iterable<String> get();
}

Why for the code above I get:

incompatible types
required: String
found:    Object

But if I will change argument of test method to A<Object>, it will compile OK? Why undefined generic parameter of variable drops explicit generic parameter of it's method's return type?

Timofey Gorshkov
  • 4,987
  • 6
  • 41
  • 66

1 Answers1

7

Why undefined generic parameter of variable drops explicit generic parameter of it's method's return type?

A is a raw type. That's a type with all generics dropped in the API dropped, even ones with fixed type arguments.

See the JLS section 4.8 and the Raw Types section of the Java Generics FAQ for more information.

In this case, if you want any A, you can use a wildcard:

public static void test(A<?> i)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hello sir, May I get your help here http://stackoverflow.com/questions/17155554/what-are-difference-of-int-initalize-values I read your post here http://bytes.com/topic/c-sharp/answers/662786-int-n-default-system-int32 can you please give us some more details ? – wuhcwdc Jun 17 '13 at 20:07
  • 1
    @PankajGarg: Please don't ask for help via comments on completely unrelated questions. Besides, you've already got plenty of answers - the three are entirely equivalent. – Jon Skeet Jun 17 '13 at 20:24