I\'m working with a TreeTable and when changing cell factory I am required to pass a
Callback, TreeTab
A wildcard represents an unknown type.
wildcard capture is the process of binding the value of a wildcard type to a new type variable. For example:
List> list = ...;
shuffle(list);
where
void shuffle(List list) {
...
}
Here, the unknown value of ? is bound to the new type variable T upon invocation of the shuffle method, allowing the shuffle method to refer to that type.
The Java compiler internally represents the value of a wildcard by capturing it in an anonymous type variable, which it calls "capture of ?" (actually, javac calls them "capture #1 of ?" because different uses of ? may refer to different types, and therefore have different captures).
Ok, so what is wrong in your code? You are trying to invoke a method
setCellFactory(Callback, TreeTableCell> factory);
with
Callback, TreeTableCell> factory;
In the method signature, the type parameter T stands for a single type, that must be provided by the caller. As a convenience, the compiler automatically attempts to infer a suitable value (-> type inference). Your compilation error means that the compiler was unable to do so.
In this instance, this is not a shortcoming of type inference, as it is actually impossible to assign a suitable value to T, because both ? need to be subtypes of T, but the compiler can not know that the two ? stand for the same type, or even related types.
To successfully invoke this method, your argument type must use the same type for all occurrences of T. If you already have such a type at hand, go ahead and use it. Otherwise, you may be able to introduce one using wildcard capture:
setCellFactory(newFactory());
where
Callback, TreeTableCell> newFactory() {
return new Callback, TreeTableCell> {
...
}
}