in javaFX Application is an abstract class. In this abstract class there have some abstract methods which are overridden in main class which extends abstract Application class and also have static launch() method in abstract class Application. The launch() method calls from the main method in main class. Now how is it possible launch() method calls these abstract methods and for these calls overridden methods in main class are executes? please help me to understand this procedure actually works. I know that non-static method can't invoked from static method and it is not possible to create instance of an abstract class. abstract method can't be a static method.
I don't want to create object of main class. Because the launch() method don't know what is the name of main class.
Write me a java code where this procedure illustrate well. like this
public class main extends Application{
public static void main(String[] args)
launch(); // launch() was a static method in Application class
}
@override
public void init(){ // init() was an abstract method in Application class
System.out.println("hello");
}
}
public abstract class Application {
public static void launch(){
init();
}
public abstract void init();
}
I want to get output : hello
The
Application#launch(String...)method is defined to:So, it does know the name of the main class by getting the "caller class". It then creates an instance of the application class via reflection and invokes the
init()andstart(Stage)methods as appropriate.The instantiation of the
Applicationimplementation1 and the invoking ofinit(),start(Stage), andstop(), are all part of the JavaFX life-cycle (documented byApplication). You don't instantiate the application class, nor do you call those methods. It's handled by JavaFX.Note you should not have your own
Applicationclass. Not only is it confusing to name a class the same as a class from the framework you're using, but it does nothing for you. You need to extendjavafx.application.Applicationif you want the normal JavaFX life-cycle.1. It's not the
Applicationclass itself that's instantiated, as it's abstract. However, you must provide a concrete subclass (i.e., implementation) as part of your project, and it's that class that's instantiated. Of course, from the point of view of JavaFX, it only knows it has an instance ofApplication. But that is basic polymorphism at work.How Does JavaFX Do It?
Here's some non-JavaFX code demonstrating the process. Note it's vastly simplified and does things slightly differently from the actual JavaFX implementation.
Then some other code can do:
Without having to pass the class to
launch.As noted, this is vastly simplified from what JavaFX actually does, and it uses some different APIs. For instance:
I used
java.lang.StackWalker(added in Java 9). JavaFX currently (as of 21-ea) still uses code that manually walks the stack trace viaThread.currentThread().getStackTrace().The JavaFX Application Thread is not created manually like I did for the "Application Event Thread" above. Instead, the FX thread is managed by the JavaFX framework, and the real launch code makes use of the internal version of
Platform#runLater(Runnable).However, the real JavaFX code does do something similar to what I did for the "JavaFX-Launcher Thread" used to invoke the
init()method.My code has the "Application Event Thread" instantiate the class, call
start(String[]), and then die (oncestartreturns). That's not how the JavaFX Application Thread works, which essentially runs in a loop.The real JavaFX code has to deal with potential
Preloaders.The real JavaFX code creates a
Stageinstance that it passes to thestart(Stage)method; I "simulate" that by passing the given command line arguments array. Though note in a real JavaFX application, if you pass the command line arguments tolaunch, then those arguments are encapsulated in aParametersobject (and rudimentarily parsed).In the real JavaFX code, the thread that calls
launchis blocked until the framework exits.If you're truly curious about the entire process, then I recommend you follow the source code.