The following code compiles unexpectedly:
import java.util.function.*;
class Test {
private String asString() {
return "bar";
}
static <T extends Test> Function<T, String> foo() {
return T::asString;
}
}
Note that the type variable T has the bound Test. As per JLS 4.4, the members of T are the members of the intersection derived from T. In this case, as there's only one class bound, the intersection type is a notional class C that extends Test. Such a notional class does _not_ feature "asString" in its members.
Note that when "asString" is called directly (e.g. w/o a method reference) the code fails as expected:
class Test {
private String asString() {
return "bar";
}
static <T extends Test> String foo(T t) {
return t.asString(); // error: "asString" has private access in Test
}
}