This code:
interface I {}
public class Test {
TableView<? extends I> table;
Test(ObservableList<? extends I> list) {
table = new TableView<>();
table.setItems(list);
}
}
...produces this message:
error: method setItems in class TableView<S> cannot be applied to given types;
table.setItems(list);
required: ObservableList<CAP#1>
found: ObservableList<CAP#2>
reason: argument mismatch; ObservableList<CAP#2> cannot be converted to ObservableList<CAP#1>
where S is a type-variable:
S extends Object declared in class TableView
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends I from capture of ? extends I
CAP#2 extends I from capture of ? extends I
How can there be a type mismatch between two identical types? What am I missing?
In Java Generics, ? wildcards cannot be identical, because they represent unknown types. An unknown type cannot equal some other unknown type.
If you want the types to match, then you must define a generic type parameter on the Test class, and refer to it throughout your class.
public class Test<T extends I> {
TableView<T> table;
Test(ObservableList<T> list) {
table = new TableView<>();
table.setItems(list);
}
}
Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error
"Since we don't know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.
On the other hand, given a List, we can call get() and make use of the result. The result type is an unknown type, but we always know that it is an object. It is therefore safe to assign the result of get() to a variable of type Object or pass it as a parameter where the type Object is expected." (http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With