Java Reference
In-Depth Information
There are basically two flavors of variables: unassignable and changeable .
Unassignable variables are declared using the def keyword and must be assigned
a default value that never changes.
public def PI = 3.14;
These variables cannot be assigned to, overridden, or initialized in object literals.
In a sense, these can be viewed as constants; however, they are not “pure” con-
stants and can participate in binding. (For more information on binding, see
Chapter 4, Synchronize Data Models—Binding and Triggers.)
Consider the following example of defining an unassignable variable that con-
tains an object. The object instance cannot change, but that does not mean the
state of that instance will not.
def centerPoint = Point{x: 100, y:100};
centerPoint.x = 500;
The actual Point object assigned to centerPoint remains unchanged, but the
state of that object instance, the actual x and y values, may change. When used in
binding though, centerPoint is constant; if the state of centerPoint changes,
the bound context will be notified of the change.
Changeable instance variables are declared using the var keyword with an
optional default value. If the default value is omitted, a reasonable default is
used; basically, Number s default to zero, Boolean defaults to false , String s
default to the empty string, Sequence s default to the Empty Sequence, and
everything else defaults to null .
Script variables are declared outside of any class declaration, whereas instance
variables are declared within a class declaration. If a script variable is declared
with one of the access modifiers— public , protected , or package —it may be
used from outside of the script file, by referring to its fully qualified name. This
fully qualified name is the combination of package name, script name, and the
variable name. The following is the fully qualified name to a public script vari-
able from the javafx.scene.Cursor class for the crosshair cursor.
javafx.scene.Cursor.CROSSHAIR;
Instance variables are declared within a class declaration and come into being
when the object is created. Listing 3.5 illustrates several examples of script and
instance variables.
Search WWH ::




Custom Search