title: How to create a Singleton MainClass in JavaFX?
body: <p>I want to create a <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow">Singleton</a> main class in <a href="http://docs.oracle.com/javafx/index.html" rel="nofollow">JavaFX</a> but I am having difficulties because the main class must extend <a href="http://docs.oracle.com/javafx/2/api/javafx/application/Application.html" rel="nofollow">Application</a>, so the constructor can't be private.</p>
  
  <p>I want the main class to be a Singleton because I want to access a couple of instance methods from any class.</p>
  
  <p>In my current code I am using the main class as a fake Singleton because I do not provide any guarantee that the class will not be instantiated again in some part of the code.</p>
  
  <p>I would like to know if there is a decent way to create a Singleton when you can't use private constructors.</p>
  
  <p>Here is the code of my main class:</p>
  
  <pre><code>public final class MainClass extends Application {
      private static MainClass instance;
      private Stage primaryStage, optionsStage;
  
      @Override
      public void start(final Stage primaryStage) {
          instance = this;
          try {
  
              // Main scene.
              {
                  Parent page = (Parent) FXMLLoader.load(
                          MainWindowController.class.getResource("main.fxml"));
                  Scene mainScene = new Scene(page);
                  primaryStage.setScene(mainScene);
                  primaryStage.show();
              }
  
              // Options scene.
              {
                  optionsStage = new Stage();
                  optionsStage.setTitle("Options");
                  optionsStage.setResizable(false);
                  optionsStage.initModality(Modality.APPLICATION_MODAL);
                  optionsStage.initOwner(primaryStage);
  
                  Parent page = (Parent) FXMLLoader.load(
                          OptionsWindowController.class.getResource("options.fxml"));
                  Scene scene = new Scene(page);
                  optionsStage.setScene(scene);
              }
  
          } catch (Exception ex) {
              Constants.LOGGER.log(Level.SEVERE, ex.toString());
          }
      }
  
      /**
       * Returns the instance of this class.
       * @return
       */
      public static MainClass getInstance() {
          return instance;
      }
  
      public Stage getPrimaryStage() {
          return primaryStage;
      }
  
      /**
       * Returns the options stage.
       * @return
       */
      public Stage getOptionsStage() {
          return optionsStage;
      }
  
      /**
       * The main() method is ignored in correctly deployed JavaFX
       * application. main() serves only as fallback in case the
       * application can not be launched through deployment artifacts,
       * e.g., in IDEs with limited FX support. NetBeans ignores main().
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>