title: How do you make a "counter" count in Java if I can't access a non-final variable from the inner class?
body: <p>I'm working on a small coding exercise for my course but I can't seem to overcome an issue I'm having with it. I want to count everytime someone clicks a button, but I can't create a variable to store the information because I have to create the variable outside the event handler so it doesn't reset to 0 everytime the button is pressed.</p>
  <p>However, whenever I create a variable outside the event handler and try to add onto it by 1, it says it cannot be accessed from within the inner class.</p>
  <pre><code>public class HelloApplication extends Application {
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage primaryStage) {
          primaryStage.setTitle(&quot;Counter Application&quot;);
          Label label = new Label(&quot;Counter&quot;);
          Button btn = new Button();
          btn.setText(&quot;Count&quot;);
           int count = 0;
  
          btn.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
              @Override
              public void handle(ActionEvent event) {
                  count++;
              }
          });
  
          StackPane root = new StackPane();
          root.getChildren().add(btn);
          primaryStage.setScene(new Scene(root, 300, 250));
          primaryStage.show();
      }
  }
  </code></pre>