Hello, everyone!
I thought, I would understand Java generics by now. But now I'm helpless again.
I have a generic class where a c-tor constructs correctly-typed instance, while a static factory method produces a type mismatch.
Please look at the following code:
public class _GenericFactoryMethods {
public final static class DemoClass<T1, T2> {
private final T1 _1;
private final T2 _2;
// static factory method
public static <T1, T2> DemoClass<T1, T2> create(T1 _1, T2 _2) {
return new DemoClass<T1, T2>(_1, _2);
}
// usual c-tor
public DemoClass(T1 _1, T2 _2) {
this._1 = _1;
this._2 = _2;
}
public T1 get1() { return _1; }
public T2 get2() { return _2; }
}
public void doSomething() {
String str = "test";
Class<? extends CharSequence> _1 = str.getClass();
CharSequence _2 = str;
// works
DemoClass<Class<? extends CharSequence>, CharSequence> dc1 =
new DemoClass<Class<? extends CharSequence>, CharSequence>(_1, _2);
// compile error
DemoClass<Class<? extends CharSequence>, CharSequence> dc2 =
DemoClass.create(_1, _2);
}
}
The error is:
Uncompilable source code - incompatible types
required: _GenericFactoryMethods.DemoClass<java.lang.Class<? extends java.lang.CharSequence>,java.lang.CharSequence>
found: _GenericFactoryMethods.DemoClass<java.lang.Class<capture#794 of ? extends java.lang.CharSequence>,java.lang.CharSequence>
Please help to understand and to solve this.
(I really need static factory methods for some of such classes).
EDIT
Can someone also explain why explicit type parameters must be used?