class Test<G extends String>{
public G test(){return null;}
public List<G> tests(){return new ArrayList<>();}
}
public void doTest(Test t){
//works fine
String str = t.test();
//Compile error: expected String found Object
str = t.tests().iterator().next();
}
I would like the last line to return a String instance instead of Object, as the type G was bound to subclass String. There is any other way than casting ?
No, the type as been erased. You're running into two different rules for type erasure - from JLS section 4.6:
So the erasure of
List<G>
isList
, but the erasure ofG
isString
- that's why the first assignment works.All you need to do to get this code to compile is use a wildcard in your parameter: