passing a parameter into a javafx Application class
I am writing a java class extending the application class of javafx and am trying to pass an object into the constructor
Do not try to pass an object into the constructor of a JavaFX Application.
Read the JavaFX Application javadoc:
The Application subclass must be declared public and must have a public no-argument constructor.
Additionally, study the application lifecycle information in the linked javadoc.
You should only have a single application instance within the VM and that will be created by the application launch sequence which invokes the no-args constructor.
Therefore creating an additional constructor for the application which has arguments is pointless at best as it should never be called.
Why you shouldn't manually create a new application instance
If your parameterized constructor were manually called via new
, yes it would create a new application instance. But then you would have two instances in the VM, one created manually by you, and one automatically created by the launcher, and they would have different states of initialization and one would be managed by the JavaFX lifecycle and one (the one you manually created) would not, which would just be an unnecessarily confusing and error-prone situation to create => you should never manually create a JavaFX Application instance.
Command line args
Note: You can provide a static main method that has a standard String[] args
argument list, and those parameters will be available to the application instance via getParameters()
, so it is possible to pass arguments to the application instance, but those arguments are a completely different thing then what you are trying to do as they are just command line string arguments, not arbitrary Java object references. The arguments are passed as parameters to the application class instance via the launch(appClass, args) method.