Non-static method cannot be referenced from a static context in java 8 streams

yuranos87 :

I was playing around with examples from http://www.concretepage.com/java/jdk-8/java-8-unaryoperator-binaryoperator-example.

What I find really confusing is that when I mistakenly put a wrong type into one of generics when forming Collectors, java compiler gives me a very misleading message:

Non-static method cannot be referenced from a static context

My error has nothing to do with static vs instance context in reality:

Map<String, Map<Integer, Integer>> mapOfStudents = list.stream().collect(Collectors.groupingBy(Student::getClassName,
            Collectors.toMap(Student::getName, Student::getAge)));

My mistake is in generic return type. When I correct it and put:

Map<String, Map<String, Integer>> mapOfStudents

everything goes back to normal.

Can someone explain the reason behind such a confusing error message? I'm sure the is a good one, but I fail to grasp it.

EDIT:

~$ java -version
openjdk version "1.8.0_121"
OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
Tagir Valeev :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Dev

Non-static method cannot be referenced from a static context when flattening map java 8

From Java

non static method cannot be referenced from a static context

From Dev

non-static method getIntent() cannot be referenced from a static context

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Dev

java: non-static variable this cannot be referenced from a static context

From Dev

Non static variable cannot be referenced from static context java

From Dev

non-static variable cannot be referenced from static context [JAVA]

From Dev

non-static variable cannot be referenced from a static context java

From Java

java - Non-static method 'getLogger' cannot be referenced from a static context

From Dev

JAVA - non-static method add(E) cannot be referenced from a static context

From Dev

Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

From Dev

Non-static method cannot be referenced from a static context - Java to Kotlin - Android

From Dev

Java non-static method cannot be refenced from a static context

From Dev

Error:non static method 'edit' cannot be referenced in static context

From Dev

Trying to get my show-method to work (Non static method cannot be referenced from static context)

From Dev

non-static variable s cannot be referenced from a static context

From Dev

non static variable this cannot be referenced from a static context

From Java

"non-static variable this cannot be referenced from a static context"?

From Java

non-static class cannot be referenced from a static context

From Dev

Non-static variable filepath cannot be referenced from a static context

From Dev

Non-static edit() cannot be referenced from a static context

From Dev

non static setGravity cannot be referenced from static context

From Dev

Non-static variable cannot be referenced from a static context?

From Java

Java - cannot be referenced from a static context

From Dev

Map.merge .. non-static method cannot be referenced from static context

From Dev

non-static method matcher(CharSequence) cannot be referenced from a static context

From Java

Why does String::isEmpty works when non-static method cannot be referenced from a static context?

Related Related

  1. 1

    Non-static method cannot be referenced from a static context in java 8 streams

  2. 2

    Non-static method cannot be referenced from a static context when flattening map java 8

  3. 3

    non static method cannot be referenced from a static context

  4. 4

    non-static method getIntent() cannot be referenced from a static context

  5. 5

    "Non-static method cannot be referenced from static context" error

  6. 6

    Non-static method getSocketFactory cannot be referenced from a static context

  7. 7

    java: non-static variable this cannot be referenced from a static context

  8. 8

    Non static variable cannot be referenced from static context java

  9. 9

    non-static variable cannot be referenced from static context [JAVA]

  10. 10

    non-static variable cannot be referenced from a static context java

  11. 11

    java - Non-static method 'getLogger' cannot be referenced from a static context

  12. 12

    JAVA - non-static method add(E) cannot be referenced from a static context

  13. 13

    Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

  14. 14

    Non-static method cannot be referenced from a static context - Java to Kotlin - Android

  15. 15

    Java non-static method cannot be refenced from a static context

  16. 16

    Error:non static method 'edit' cannot be referenced in static context

  17. 17

    Trying to get my show-method to work (Non static method cannot be referenced from static context)

  18. 18

    non-static variable s cannot be referenced from a static context

  19. 19

    non static variable this cannot be referenced from a static context

  20. 20

    "non-static variable this cannot be referenced from a static context"?

  21. 21

    non-static class cannot be referenced from a static context

  22. 22

    Non-static variable filepath cannot be referenced from a static context

  23. 23

    Non-static edit() cannot be referenced from a static context

  24. 24

    non static setGravity cannot be referenced from static context

  25. 25

    Non-static variable cannot be referenced from a static context?

  26. 26

    Java - cannot be referenced from a static context

  27. 27

    Map.merge .. non-static method cannot be referenced from static context

  28. 28

    non-static method matcher(CharSequence) cannot be referenced from a static context

  29. 29

    Why does String::isEmpty works when non-static method cannot be referenced from a static context?

HotTag

Archive