Dr. Vatsal Shah
Subject Material

Java · JavaFX · Topic 2

JavaFX Application Lifecycle

Follow a JavaFX application from construction and launch through init(), start(), event processing, and stop().

Topic progress · 1 of 6 sections

Step 1 of 6

2.1 Application Class

Learning objective

Explain why a JavaFX program extends Application.

The Application class controls the JavaFX lifecycle. A normal application creates a subclass and overrides start(Stage stage).

Application subclass
public class StudentApp extends Application {
    @Override
    public void start(Stage stage) {
        // Build and show the interface here.
    }
}
Check your understanding: Which class does a normal JavaFX application extend?

javafx.application.Application.

Step 2 of 6

2.2 main() Method

Learning objective

Explain the role and optional nature of main() in a JavaFX application.

Conventional main method
public static void main(String[] args) {
    Application.launch(args);
}

Some Java launch configurations can start an Application subclass without an explicit main() method. Including main() remains clear and portable for classroom programs.

Check your understanding: What should main() normally do in a JavaFX program?

Call Application.launch(args).

Step 3 of 6

2.3 launch() Method

Learning objective

Describe what happens when the JavaFX runtime is launched.

Application.launch() starts the JavaFX runtime, creates the Application instance, calls init(), and schedules start(Stage) on the JavaFX Application Thread.

Launch the runtime
Application.launch(args);
Check your understanding: Can Application.launch() be called repeatedly in the same JVM?

No. A JavaFX application launch is intended to occur once per JVM process.

Step 4 of 6

2.4 init() Method

Learning objective

Use init() for non-visual preparation and distinguish its thread from the UI thread.

Prepare non-visual state
private String message;

@Override
public void init() {
    message = "Application initialized";
}

Suitable work for init()

  • Read configuration
  • Prepare plain data
  • Create non-visual services
  • Validate startup arguments
Check your understanding: Should controls be created and added to the Scene in init()?

No. Scene Graph work belongs on the JavaFX Application Thread, normally inside start().

Step 5 of 6

2.5 start(Stage primaryStage)

Learning objective

Build the scene graph and display the primary window.

JavaFX calls start(Stage primaryStage) after init(). The supplied primary stage is the main window.

Build the interface in start()
@Override
public void start(Stage primaryStage) {
    Label label = new Label(message);
    Scene scene = new Scene(new VBox(10, label), 360, 160);
    primaryStage.setScene(scene);
    primaryStage.show();
}
Check your understanding: On which thread does JavaFX call start()?

The JavaFX Application Thread.

Step 6 of 6

2.6 stop() Method

Learning objective

Perform cleanup when a JavaFX application terminates and trace the complete lifecycle.

LifecycleDemo.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class LifecycleDemo extends Application {
    private String message;

    @Override
    public void init() {
        message = "Ready";
        System.out.println("init()");
    }

    @Override
    public void start(Stage stage) {
        System.out.println("start()");
        stage.setScene(new Scene(
                new VBox(10, new Label(message)), 300, 140));
        stage.setTitle("Lifecycle Demo");
        stage.show();
    }

    @Override
    public void stop() {
        System.out.println("stop()");
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
Expected result
Console after launch:
init()
start()

Window: Lifecycle Demo displaying "Ready"

Console after the window closes:
stop()
Lifecycle order
PhaseResponsibility
ConstructionJavaFX creates the Application object
init()Prepare non-visual state
start(Stage)Build and show the interface
Event loopRespond to user actions
stop()Release resources before exit
Check your understanding: Which method should close application-owned files or network resources during shutdown?

stop().