4

I have written a little test application that looks like this:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;

public class Test extends Application {

    public Test() {
        System.out.println("first");
    }

    @Override
    public void init() throws Exception {
        System.out.println("second");
        super.init();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        System.out.println("third");
        Platform.exit();
    }

    @Override
    public void stop() throws Exception {
        System.out.println("fourth");
        super.stop();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The output is:

first
second
third
fourth

Now, I am asking myself where the difference is between using the default constructor or the init method for initialising some required things.

Thanks!

P.S.: This question is not duplicate with this one, because my issue does not handle the initialisation of variables. My question is JavaFX specific!

Community
  • 1
  • 1
mrbela
  • 4,477
  • 9
  • 44
  • 79
  • 3
    Possibly the only difference is that in `init` you can get the parameters - see [here](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html) and [here](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html#getParameters--) – Itai Jan 14 '16 at 09:45

3 Answers3

7

When the Application is launched, the following steps are executed during the launch process:

  1. The instance of the Application subclass that should be launched is created. (Constructor is called)
  2. The init() of that instance is called.
  3. The start method of that instance is called.

See javadoc for Application

Since you exit the application after the start method is called, the stop method of the instance is called.

Note that there is no need to use super.init() and super.stop():

From the javadoc

The init and stop methods have concrete implementations that do nothing.



The differences of constructor, init and start are:

Constructor

If your application is not final the application class could be extended. Since the constructor of the extending class is not complete, it may be dangerous to pass a reference to the class to other classes in this state. Also the instance hasn't been initialized to a point where the getParameters method returns the parameters.

init

Initialisation should generally be done in this method. This method isn't run from the fx application thread, so some things cannot be done from this method, like creating a Stage.

start

Last step in the initialisation. Do things that require to be run from the application thread here. Be careful not to block this thread with long-runing operations however, since this freezes the GUI.

fabian
  • 80,457
  • 12
  • 86
  • 114
0

tl;dr

difference is between using the default constructor or the init

init runs on a separate thread.

Threading

See the Threading section of the Javadoc.

The main method of any Java app executes on an initial thread assigned to that app.

In a JavaFX app, the construction of your Application subclass occurs on another thread, a thread named JavaFX Application Thread. This thread is the event-dispatching thread for your JavaFX app. Any Stage and Scene objects, and their contained JavaFX objects, must only be accessed and manipulated from this particular thread.

After construction of your Application subclass, the JavaFX framework automatically invokes the Application#init on yet another thread, a third thread, a “launcher thread”.

While that init method executes on the other thread, the JavaFX framework executes the Application#start method. The crucial point here is that start runs alongside init, not after init completes. The execution of init and start can overlap in time.

So at least three threads are involved in getting your JavaFX/OpenJFX app going.


See a my table of sequence steps of a JavaFX app’s lifecycle in my Answer on a related Question.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

Application Class having init() method which you have override. The Init method will call immediately after the Test class construction. Init method is actually an internal method of Application Class. This method is actually initialize the whole FX application components internally.

Application class JavaDoc

it may construct other JavaFX objects in this method.

Zico
  • 2,349
  • 2
  • 22
  • 25