tags:

views:

48

answers:

1

Is it possible to create a constructor with arguments on a JavaFX class?

This can be achieved in Java by:

   MyObj(String foo, String bar) {
       // ... 
   }

Is this possible in JavaFX?

i.e.

   // creating an Object Literal without the specified arguments must result in compile error
   var myObj = MyObj {} ;

   // While this should not result in compile error
   var myObj2 = MyObj {foo: "foo", bar: "bar"};
+1  A: 

The short answer is "no" - it is perfectly legal to create an instance of a class with no properties set on it - you can't force a compilation error.

I don't know your use case, but you could use a postinit block to set various defaults after object creation if that is suitable, or use a regular Java object - you can of course instantiate Java objects from JavaFX, and they are subject to compilation checks

Matthew Hegarty
Thanks, I'll go with your later advice to just instantiate the Java Object in my JavaFX Script
Joopiter