0

First, all my action listeners call a private method in my class. They are in the initComponents method that is not static, and so are the private method called by the action listeners.

The project was on Java 1.5 and moved to Java 1.8. SonarLint didn't like those statements :

someButton.addActionListener(new java.awt.event.ActionListener() {
  public void actionPerformed(java.awt.event.ActionEvent evt) {
    doSomeAction(evt);
  }
});

At first, SonarLint propose to change those assignment using lambdas (Squid:S1604).

I changed those with:

someButton.addActionListener(e -> doSomeAction(e));

Then the strange Squid:S1612 occured: it propose to use method references instead. Here is what I did:

someButton.addActionListener(ClassName::doSomeAction);

Then Eclipse Oxygen said that this is an error and won't compile The error message is:

Cannot make a static reference to the non-static method doSomeAction(ActionEvent) from the type ClassName

So the question is: can I really use method reference or should I just stick with lambda?

  • When `doSomeAction` is an instance method, you need `this::doSomeAction`. – Holger Feb 15 '18 at 16:16
  • I made some other changes since one other *SolarLint* warning said that the `ActionEvent` param wasn't used in the method `doSomeAction`. I removed them and now the Squid:S1612 disappeared since the method references needs a perfect match in the prototype (method signature). Just for fun, I tried your solution by adding back the param and it worked. Thanks a lot ! – André Jacques Feb 15 '18 at 17:09
  • Possible duplicate of [Cannot make a static reference to the non-static method](https://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-method) – Jens Feb 08 '19 at 09:38

0 Answers0