I couldn't event choose right words for the question.
I have a class and a factory method.
class MyObject<Some, Other> {
static <T extends MyObject<U, V>, U, V> T of(
Class<? extends T> clazz, U some, V other) {
// irrelevant
}
private Some some;
private Other other;
}
Now I want to add two more factory methods each which only takes U or V.
That would be look like this. That means those omitted V, or U remains unset(null).
static <T extends MyObject<U, ?>, U> T ofSome(
Class<? extends T> clazz, U some) {
// TODO invoke of(clazz, some, @@?);
}
static <T extends MyObject<?, V>, V> T ofOther(
Class<? extends T> clazz, V other) {
// TODO invoke of(clazz, @@?, other);
}
I tried this but not succeeded.
static <T extends MyObject<U, ?>, U> T ofSome(
final Class<? extends T> clazz, final U some) {
return of(clazz, some, null);
}
Compiler complains with following message.
no suitable method found for of(java.lang.Class,U, <nulltype>)
What is the right way to do it?
You still need the generic parameter
V, you just don't need the method parameterV.You need
VinofSomeso that the compiler can infer theVtype forofby looking at the type ofclazz.