Handling Events with Lambda Expressions

Lambda Expressions and Functional Interfaces are a new feature of Java 8 and the support provided for lambda expressions is only with functional interfaces. A functional interface is an interface that contains only one abstract method. This is exactly the case with handling events in Java and more accurately for these examples for handling events using the JavaFX library. Prior to lambda expressions the code execution for a button click would go this way:
button.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent actionEvent) {
        String newColor = "rgba(100, 0, 100)";
        stackPane.setStyle("-fx-background-color: " + newColor);
    }
});
Here is created an anonymous class that implements the EventHandler interface and it is being passed to the method setOnAction. The code that will be executed whenever the button is fired is placed within the handle method which is the only method in the EventHandler interface which makes it a functional interface. With lambda expressions the code would look like this:
button.setOnAction((ActionEvent event) -> {
        String newColor = "rgba(100, 100, 0)";
        stackPane.setStyle("-fx-background-color: " + newColor);
});
In this case the compiler can infer the name of the method because it is the only method in the interface so the body and the parameter declaration is enough.

Syntax

The syntax for multiple parameters and one line of execution is:
(parameters) -> expression
Executing multiple lines:
(parameters) -> { statements; }

Best Practices

Lambda expressions should be simplified as much as possible. For example the event type declaration from the previous example code can be removed because the EventHandler expects an ActionEvent and the type of the event reference can also be inferred making the code like this:
button.setOnAction((event) -> {
        String newColor = "rgba(100, 100, 0)";
        stackPane.setStyle("-fx-background-color: " + newColor);
});
Because parentheses are not mandatory when there is one parameter passed, another improvement would be to remove them as well:
button.setOnAction(event -> {
        String newColor = "rgba(100, 100, 0)";
        stackPane.setStyle("-fx-background-color: " + newColor);
});
If there is only one line of code with a return statement in the method body, the return statement can also be removed.

“Effectively Final” Variables

Accessing a non-final variable inside lambda expressions will cause a compile-time error. This doesn’t mean that every variable should be declared final. Acording to the term every variable which value is assigned only once is considered to be “effectively final” by the compiler.
For the purpose of the example the variable color is defined in the method start which is where the lambda expression is defined too.
public void start(Stage primaryStage) {
    String color = "rgba(0, 100, 100)";
Trying to assign a value to color will pop-up the message: “Variable ‘color’ is already defined in the scope.”
button.setOnAction((ActionEvent event) -> {
    String color = "rgba(100, 100, 0)";
    stackPane.setStyle("-fx-background-color: " + color);
});
Running it will produce the error:
Error:(33,20) java: variable color is already defined in method start(javafx.stage.Stage)