Can someone explain this behaviour to me:
Note: That T is never used in SomeThingGeneric
public static class SomeThingGeneric<T> {
public List<String> getSomeList() {
return null;
}
}
final SomeThingGeneric<Object> someThingGenericObject = new SomeThingGeneric<Object>();
final SomeThingGeneric<?> someThingGenericWildcard = new SomeThingGeneric<Object>();
final SomeThingGeneric someThingGenericRaw = new SomeThingGeneric<Object>();
for (final String s : someThingGenericObject.getSomeList()) { } // 1 - compiles
for (final String s : someThingGenericWildcard.getSomeList()) { } // 2 - compiles
for (final String s : someThingGenericRaw.getSomeList()) { } // 3 - does not compile!
(1) and (2) compiles but (3) fails with following message:
incompatible types
found : java.lang.Object
required: java.lang.String
If anyone wants the full code, here it is. I have verified this in both Java 5 and 6.