Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does javafx main method launch(args) work?

The methods outside the main method are not mentioned in main method. The main method only contain launch(args);

I thought it need to call the methods outside the method that make it work in the program?

So how does launch(args) work?

like image 551
user2709845 Avatar asked Aug 01 '14 00:08

user2709845


People also ask

What does launch do in JavaFX?

The Java launcher loads and initializes the specified Application class on the JavaFX Application Thread. If there is no main method in the Application class, or if the main method calls Application. launch(), then an instance of the Application is then constructed on the JavaFX Application Thread.

Which method is automatically called by the launch method in a JavaFX program?

In the main method, you have to launch the application using the launch() method. This method internally calls the start() method of the Application class as shown in the following program. Prepare a scene graph with the required nodes.

Which JavaFX application lifecycle method is invoked when application's instance is created?

An instance of the application class is created. Init() method is called. The start() method is called. The launcher waits for the application to finish and calls the stop() method.

Can a JavaFX be run without main method?

You can actually launch a JavaFX application without a main() method.


1 Answers

If you open a template of JavaFX in Netbeans it have a JavaDoc explaining it:

/**
 * 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);
}

So essentially, it shouldn't even be called as it is JavaFX, however if it is called then it will just pass the command line arguments (args) to the javafx.application.Application.launch which will open the JavaFX as expected.

In case you are still wondering how does the launch works, then you probably should check its source code.

like image 62
Mansueli Avatar answered Sep 21 '22 03:09

Mansueli