Given the following Java code:
public class Test {
public static class A<T> {
private T t;
public A(T t) {
this.t = t;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
public static class B<T> {
private T t;
public B(T t) {
this.t = t;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
public static class F<T> {
private T t;
public F(T t) {
this.t = t;
}
public A<B<T>> construct() {
return new A<>(new B<>(t));
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
public static void main(String[] args) {
F<?> f = new F<>(0);
// 1: KO
// A<B<?>> a = f.construct();
// 2: KO
// A<B<Object>> a = f.construct();
// 3: OK
// A<?> a = f.construct();
}
}
In the main method of the Test
class, what is the correct type of a variable that will receive the result of f.construct()
?
This type should be something like A<B<...>>
where ...
is what I'm looking for.
There are 3 commented lines of code above which represent my attempts to solve this problem.
The first and second lines aren't valid.
The third is but I loose the B
type information and I have to cast a.getT()
.
A<? extends B<?>> a = f.construct();
is the right syntax, as stated by Paul Boddington.