title: JavaFX FXML controller - constructor vs initialize method
body: <p>My <code>Application</code> class looks like this:</p>
  
  <pre><code>public class Test extends Application {
  
      private static Logger logger = LogManager.getRootLogger();
  
      @Override
      public void start(Stage primaryStage) throws Exception {
  
          String resourcePath = "/resources/fxml/MainView.fxml";
          URL location = getClass().getResource(resourcePath);
          FXMLLoader fxmlLoader = new FXMLLoader(location);
  
          Scene scene = new Scene(fxmlLoader.load(), 500, 500);
  
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>The <code>FXMLLoader</code> creates an instance of the corresponding controller (given in the <code>FXML</code> file via <code>fx:controller</code>) by invoking first the default constructor and then the <code>initialize</code> method:</p>
  
  <pre><code>public class MainViewController {
  
      public MainViewController() {
          System.out.println("first");
      }
  
      @FXML
      public void initialize() {
          System.out.println("second");
      }
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>first
  second
  </code></pre>
  
  <p>So, why does the <code>initialize</code> method exist? What is the difference between using a constructor or the <code>initialize</code> method to initialize the controller required things?</p>
  
  <p>Thanks for your suggestions!</p>