-3

I am learning Javafx and wondering why this calling launch(args) in this code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;


public class Gui extends Application{

    @Override
    public void start(Stage primaryStage){
        Button btn = new Button("OK");
        Scene scene = new Scene(btn, 200, 250);
        primaryStage.setTitle("My First GUI");
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setResizable(true);
    }

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

}

is equivalent when we call

launch(args);

I've searched and found this answer "the JavaFX main class is a subtype of Application." but I can't understand it.

Eugene Primako
  • 2,767
  • 9
  • 26
  • 35
  • See https://stackoverflow.com/questions/10291949/are-static-methods-inherited-in-java – Slaw Aug 02 '18 at 22:40

1 Answers1

1

It's because you extended your gui class with javafx Application class. In other words, you inherited all of its methods including static void launch.

Joe Vanjik
  • 357
  • 2
  • 7
  • I know that in javafx is not allowed but if we remove extends Application we should declare Application.launch(args) explicitly ? – Nader Abd Alhamed Aug 02 '18 at 22:28
  • 1
    From the docs of the two Application.launch() overloads, https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html#launch-java.lang.Class-java.lang.String...- , When you call the Application.launch(args) method, it assumes that the Application subclass to launch JavaFX from is the class containing that method call. This means that the class with the call must extend Application. If you want to launch a different application class, use the first method and pass the desired class. Something like `Application.launch(Gui.class, args)` – JoseRivas1998 Aug 02 '18 at 22:42