Joopiter
Joopiter

Reputation: 535

Constructor with arguments on JavaFX?

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"};

Upvotes: 1

Views: 1361

Answers (1)

Matthew Hegarty
Matthew Hegarty

Reputation: 4306

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

Upvotes: 1

Related Questions