The Application Class & Lifecycle
The Application Class & Lifecycle
Every JavaFX program revolves around a single abstract class: javafx.application.Application. Subclassing it and implementing its abstract method start() is the minimum contract required to put a window on screen. But the Application class does far more than just call start() — it manages a carefully sequenced lifecycle that initialises the toolkit, hands off to your code on the correct thread, and tears everything down cleanly when the user closes the last window. Understanding that lifecycle is what separates a JavaFX developer from someone who merely copied a "Hello World" example.
The Three Lifecycle Methods
Application defines three overridable methods that are called in a guaranteed order:
init()— called once on the JavaFX Launcher thread before the GUI is created. Override this to load configuration, open database connections, or do any heavyweight work that does not touch UI nodes. The default implementation does nothing.start(Stage primaryStage)— called on the JavaFX Application Thread (the UI thread). This is the only abstract method; you must implement it. By the time it is called the toolkit is fully initialised and it is safe to create and showStageandSceneobjects. TheprimaryStageparameter is the main window provided by the platform.stop()— called once on the JavaFX Application Thread after the last window closes (or afterPlatform.exit()is invoked). Override this to release resources — close file handles, stop background threads, flush caches. The default implementation does nothing.
init() runs on the launcher thread — you cannot create Stage or any UI node there. start() and stop() run on the JavaFX Application Thread — all GUI operations belong here. Violating these rules causes IllegalStateException at runtime.
The launch() Method
Your main() method's only real job is to call Application.launch(). This static method:
- Starts the JavaFX toolkit and its event-dispatch thread.
- Instantiates your
Applicationsubclass (using its no-argument constructor). - Calls
init()on the launcher thread. - Calls
start(primaryStage)on the JavaFX Application Thread. - Blocks until the application exits.
- Calls
stop()on the JavaFX Application Thread.
The most common form passes the class literal and any command-line arguments:
When called from within the Application subclass itself, the overload that omits the class is equivalent:
--add-modules javafx.controls,javafx.fxml to the JVM. The Application.launch() call itself is unchanged — the change is in how you start the JVM, not in the code.
A Minimal Application — Annotated
The listing below is the smallest complete JavaFX program. Read each comment carefully; every line has a specific reason to exist.
Passing Parameters to Your Application
Any strings passed after the class name on the command line (or through launch(args)) are accessible inside init() and start() via getParameters(). This is JavaFX's built-in mechanism for reading launch-time configuration without reaching for System.getenv() or a static field.
Clean Shutdown
JavaFX exits automatically when the last window is closed, but you have fine-grained control:
Platform.setImplicitExit(false)— prevents automatic exit when the last window closes. Useful for system-tray apps that hide their window without quitting.Platform.exit()— requests an orderly shutdown. It triggersstop()and then terminates the JavaFX Application Thread. Call this from any thread.- Calling
System.exit()skipsstop()entirely — avoid it unless you are in an unrecoverable error state.
System.exit() as a normal quit path. It bypasses stop(), so any cleanup code you placed there — flushing writes, releasing locks, saving state — will silently be skipped. Always use Platform.exit() to close a JavaFX application gracefully.
Lifecycle in Practice — a Realistic Template
Real applications override all three lifecycle hooks. Here is a template you can copy and adapt:
Run this and watch the console — you will see exactly which thread each method executes on, confirming the lifecycle sequence you learned above.
Summary
The Application class is the entry point and lifetime owner of every JavaFX program. init() handles pre-GUI setup on the launcher thread; start() builds and shows the UI on the Application Thread; stop() releases resources when the application closes. Application.launch() orchestrates all of this from your main() method. Keeping heavyweight work in init(), UI work in start(), and cleanup in stop() gives you a clean, predictable application structure from day one. In the next lesson you will explore the Stage and Scene objects that start() receives and creates.