17

My original code is this:

private static void onClicked(MouseEvent event) {
    // code to execute
}

// somewhere else in the program:
setOnMouseClicked(event -> SomeClass.onClicked(event));

But IntelliJ says "Can be replaced with method reference" which I'm not too sure how to do. I thought I would do this:

setOnMouseClicked(event -> SomeClass::onClicked);

But then that tells me "void is not a functional interface", but I don't want to return anything. I just want the handler to execute. How can I fix this?

Thank you!

1 Answer 1

40

You are mixing a lambda expression with a method reference.

Change

setOnMouseClicked(event -> SomeClass::onClicked);

to

setOnMouseClicked(SomeClass::onClicked);
Sign up to request clarification or add additional context in comments.

2 Comments

@Mayron FIY, IntelliJ can change a lambda expression with method reference for you (when it is possible). Just use alt+enter shortcut on expression
@Prim Thanks, just tried it out. Very useful to know!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.