Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:22:10+00:00 2026-06-16T01:22:10+00:00

I want to create a Singleton main class in JavaFX but I am having

  • 0

I want to create a Singleton main class in JavaFX but I am having difficulties because the main class must extend Application, so the constructor can’t be private.

I want the main class to be a Singleton because I want to access a couple of instance methods from any class.

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.

I would like to know if there is a decent way to create a Singleton when you can’t use private constructors.

Here is the code of my main class:

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:22:11+00:00Added an answer on June 16, 2026 at 1:22 am

    I’d think that adding a public constructor like the following and removing the instance = this; assignment in your start method would do the trick, although I am not well-versed in JavaFX. I don’t think it would ever try to instantiate your main class more than once, but if the API makes no such guarantee, you might find yourself in trouble later on down the road.

    In any case… you’d find out pretty darn quickly if you did something like the following:

    public MainClass(){
        super();
        synchronized(MainClass.class){
            if(instance != null) throw new UnsupportedOperationException(
                    getClass()+" is singleton but constructor called more than once");
            instance = this;
        }
    }
    

    NetBeans’ compiler (if you’re using it) will whine about a “leaking this“, but this will guarantee that you don’t run the constructor to completion more than once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want create an application with animate button? how can i do? after click
There is some interfaces in my app and I want to create singleton class,
I want to create a class that can add methods dynamically and allow multiple
I want create a excel with Apache POI in java and I must insert
I want to create a singleton that remains alive for the life of the
I want to create a singleton object in Ext JS 4. Here is my
I am writing an iPhone app and I want to create a NSCache singleton.
How should I write a copy constructor for my singleton class to prevent the
I don't want to create a discussion about singleton better than static or better
I want to create a utility class that loads images contained in the assets/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.