title: How can I rewrite an anonymous Java class into a non-anonymous class?
body: <p>Learning about anonymous classes and have this from Oracle.  I am hoping someone can show me what this would look like if I did not use anonymous classes.  How would I have done this with a new class?</p>
  
  <pre><code>import javafx.event.ActionEvent;
  import javafx.event.EventHandler;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.StackPane;
  import javafx.stage.Stage;
  
  public class HelloWorld extends Application {
      public static void main(String[] args) {
          launch(args);
      }
  
      @Override
      public void start(Stage primaryStage) {
          primaryStage.setTitle("Hello World!");
          Button btn = new Button();
          btn.setText("Say 'Hello World'");
          btn.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
              @Override
              public void handle(ActionEvent event) {
                  System.out.println("Hello World!");
              }
          });
  
          StackPane root = new StackPane();
          root.getChildren().add(btn);
          primaryStage.setScene(new Scene(root, 300, 250));
          primaryStage.show();
      }
  }
  </code></pre>
  
  <blockquote>
    <p>In this example, the method invocation btn.setOnAction specifies what happens when you select the Say 'Hello World' button. This method requires an object of type EventHandler. The EventHandler interface contains only one method, handle. <strong>Instead of implementing this method with a new class</strong>, the example instead uses an anonymous class expression. Notice that this expression is the argument passed to the btn.setOnAction method.</p>
  </blockquote>
  
  <p>Source: <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html</a></p>