A
A
Anton2015-06-11 08:33:32
OOP
Anton, 2015-06-11 08:33:32

Where is the best place to write the main method in JavaFX?

There are Oracal tutorials .
There are simple examples in which the application is built like this:

public class ColorfulCircles extends Application {

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

    @Override
    public void start(Stage primaryStage) {

// код GUI

        primaryStage.show();
    }
}

However, when I was wooling the stackoverflow, I came across in the comments a remark from one American. He recommended moving the main method to a separate class. Like, it's more correct:
Main-class:
import javafx.application.Application;

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

And the app itself:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class TaskManagerApp extends Application {

    @Override
    public void start(Stage primaryStage) {

        Scene scene = new Scene(new Label("Hello world"), 420, 500);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Attention a question: what approach is more correct?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
halogen, 2015-06-19
@fattan

It's bad to use derogatory names, especially when the uncle gives good advice, yu know. The author on SO means that you need to separate the concepts of entry point and application. That is, there must be an entry point, and there must be an application. Separately. The advantage of this approach is that the SRP is better respected, and the application can also be used in other "cleaner" entry points: whether it's Android or GWT (but it depends on the coolness of understanding and implementing OOP), or ordinary unit tests. The Application class can be considered a facade in terms of design patterns. JavaFX has nothing to do with it.
----
Notes in the margins
In textbooks, they try to write as simple a code as possible to make it easier to understand and reinforce the material. Therefore, main(String... args) occurs almost everywhere in such cases.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question