There is a button and a label of the counter, it is necessary that when you press the button, the counter adds 1 and displays the new value. I can not understand where I need to enter a variable, I tried both inside a lambda, and as a separate class, and with setOnAction, but the console issues "local variables referenced from a lambda expression".

myBtn.setOnAction(e -> { //логика }); 

    1 answer 1

    If we use a variable inside an anonymous class, then it must be final . This is true for lambda, because the latter, in fact, are also such.

    There are several ways to circumvent this limitation:

    • declare a final array of size 1 with the specified type
    • use a mutable object, in your case declare a variable of type AtomicInteger

    The variable must be a class field.