Classes allow for the creation of user-defined types. The JavaFX Script programming language supports multiple inheritance, and as such, defines some new terminology and rules:
The syntax for specifying a class is the class
keyword followed by the class name, optionally the extends
keyword, and a comma
separated list of the names of base classes, an open curly brace, a list of attributes and functions that each end in a semicolon, and a closing curly brace.
Import statements behave the same as in the Java programming language. The syntax is:
import PackageName.ClassName;or
import PackageName.*;
If import statements are present, they must appear before
any other application code. The JavaFX Script programming language defines its own namespace
for its built-in library classes (packages javafx.*
), but it is also possible to import standard Java programming language classes as well:
import java.lang.System; import javafx.ui.*; Frame { visible: true content: FlowPanel { content: Button { text: "Click Me" action: function() { System.out.println("Click!"); } } } }
Access modifiers on classes behave the same as in Java programming language. Classes may be declared as public
,
protected
, or private
, which allows them to be accessed anywhere, in the current package or in derived classes, or only in the
current class, respectively.
Note: The JavaFX Script programming language does not support constructors. To mimic the
behavior of a constructor, define a static
function that returns a new object,
and invoke that function instead.
As described in the previous chapter, the preferred way to instantiate a class is with an object literal. This form of object allocation uses a declarative syntax consisting of the name of the class followed by a curly brace delimited list of attribute initializers. Each initializer consists of the attribute name followed by a colon, followed by an expression which defines its value.
It is possible to use the new
keyword when creating an object,
but this should be saved for cases where instantiating a Java programming
language class is not possible via object literal syntax:
import java.io.File; var tmpPath = "/home/users/docs/tmp.txt" var myFile = new File("tmp.txt");
1. A plain class is currently translated into a Java class, while a compound class is translated to a Java class and a Java interface.