1 Reply Latest reply on Jul 7, 2019 11:25 PM by bouye-JavaNet

    How method is invoked without object creation?

    1003967

      Hi,

      I am new to JavaFX. Though I understand what's happening in the following code, one thing baffles me.

      The method getHBox() is referenced from start() method without any object creation. How can a

      non-static method be invoked this way?

       

      public class Main extends Application {

          @Override

          public void start(Stage primaryStage) {

             

              try {

                  BorderPane rootPane = new BorderPane();

                 

                rootPane.setTop(getHBox());

                 

                  Scene scene = new Scene(rootPane,400,400);

                  scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

                  primaryStage.setScene(scene);

                  primaryStage.show();

              } catch(Exception e) {

                  e.printStackTrace();

              }

          }

         

          public HBox getHBox()

          {

              HBox hb = new HBox(15);

              hb.getChildren().add(new Button("Press"));

              return hb;

          }

       

          public static void main(String[] args) {

              launch(args);

          }

      }

      If I do something like this, i.e. create an object and then call objectName.getHBox(), the exact same results

      obtained:

       

      Main m = new Main();

      rootPane.setTop(m.getHBox());

       

      What is going on in these two cases?

       

      Bye.

        • 1. Re: How method is invoked without object creation?
          bouye-JavaNet

          Hi,

          start() is not a static method, it's just an instance method that is called by the application launcher (com.sun.javafx.application.LauncherImpl an inner classs of the JavaFX API that takes care of initilizing the toolkit before starting the app) after an instance of the Main class has been created. So you have no issue calling other instance method in here.