let's consider following code:
class ArrayList<T> {}
interface MyInterface {
<U> ArrayList<U> abstractMethod(Test53 tester);
}
public class Test53 {
ArrayList<?> methodBeingReferenced() {
ArrayList<?> result = new ArrayList<>();
return result;
}
public static void main(String argv[]) {
consumeMethodReference(Test53::methodBeingReferenced);
}
static <T> ArrayList<T> consumeMethodReference(MyInterface sam) { return sam.abstractMethod(new Test53()); }
}
on JDK9b40 it causes following compilation errors:
D:\langworks\smalltests2\src\Test53.java:13: error: method consumeMethodReference in class Test53 cannot be applied to given types;
consumeMethodReference(Test53::methodBeingReferenced);
^
required: MyInterface
found: Test53::me[...]enced
reason: cannot infer type-variable(s) T
(argument mismatch; bad return type in method reference
ArrayList<?> cannot be converted to ArrayList<U>)
where T,U are type-variables:
T extends Object declared in method <T>consumeMethodReference(MyInterface)
U extends Object declared in method <U>abstractMethod(Test53)
D:\langworks\smalltests2\src\Test53.java:13: error: invalid method reference
consumeMethodReference(Test53::methodBeingReferenced);
^
non-static method methodBeingReferenced() cannot be referenced from a static context
2 errors
I believe the second error: "non-static method methodBeingReferenced() cannot be referenced from a static context" is not relevant and the fact that this message is printed here is a javac bug.
class ArrayList<T> {}
interface MyInterface {
<U> ArrayList<U> abstractMethod(Test53 tester);
}
public class Test53 {
ArrayList<?> methodBeingReferenced() {
ArrayList<?> result = new ArrayList<>();
return result;
}
public static void main(String argv[]) {
consumeMethodReference(Test53::methodBeingReferenced);
}
static <T> ArrayList<T> consumeMethodReference(MyInterface sam) { return sam.abstractMethod(new Test53()); }
}
on JDK9b40 it causes following compilation errors:
D:\langworks\smalltests2\src\Test53.java:13: error: method consumeMethodReference in class Test53 cannot be applied to given types;
consumeMethodReference(Test53::methodBeingReferenced);
^
required: MyInterface
found: Test53::me[...]enced
reason: cannot infer type-variable(s) T
(argument mismatch; bad return type in method reference
ArrayList<?> cannot be converted to ArrayList<U>)
where T,U are type-variables:
T extends Object declared in method <T>consumeMethodReference(MyInterface)
U extends Object declared in method <U>abstractMethod(Test53)
D:\langworks\smalltests2\src\Test53.java:13: error: invalid method reference
consumeMethodReference(Test53::methodBeingReferenced);
^
non-static method methodBeingReferenced() cannot be referenced from a static context
2 errors
I believe the second error: "non-static method methodBeingReferenced() cannot be referenced from a static context" is not relevant and the fact that this message is printed here is a javac bug.