First it should be noted, that the message is issued not by java compiler (javac), but by IntelliJ IDEA. You can see javac messages in "Messages Build" window when you actually launch a build process. What you see in editor window is messages generated by IDEA itself and they could differ.
The error message is misleading due to implementation of method reference resolution in IntelliJ IDEA. It considers non-static method reference to be resolved only if number of corresponding SAM (single abstract method) arguments equals to number of method arguments plus one and the first SAM argument type is compatible with method containing class. See the implementation (also isSecondSearchPossible
method above, some additional magic is performed for varargs methods).
It works correctly if your program has no errors. However if you have a mismatched type, the generic arguments of the Function
passed into toMap
cannot be substituted, so it remains Function<T, R>
, and its apply
method first argument is simply T
which does not correspond to the type Student
. Thus so-called "second search" fails and IDEA thinks that the method is referenced from static context. While both static and non-static context are not applicable here, non-static context matches your method better, at least according to the number of arguments as getName()
method receives no arguments. On the other hand, IDEA logic is "if non-static context is not applicable, then it's a static context", hence the error message.
I would consider this as a bug, or at least as a usability problem. I've just logged it here based on similar question. Hopefully we will fix it.
Disclaimer: I'm IntelliJ IDEA developer.
Update: fixed in IDEA 2017.2.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…