Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How method is invoked without object creation?

1003967Jul 2 2019 — edited Jul 7 2019

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.

Comments
Post Details
Added on Jul 2 2019
1 comment
205 views