JavaFX is a statically type-safe and type-strict scripting language. Therefore, variables (and anything which can be assigned to a variable, including functions and expressions) in JavaFX, must be associated with a type, which indicates the expected behavior and representation of the variable. This sections explores how to create, initialize, and update JavaFX variables.
Before we look at creating and using variables, it is beneficial to have an understanding of what is meant by data type and be familiar with some common data types such as String, Integer, Float
, and Boolean
. If you have written code in other scripting languages such as ActionScript, Python, and Ruby, you will find the concepts in this recipe easy to understand.
JavaFX provides two ways of declaring variables including the def
and the var
keywords.
def X_STEP = 50; prntln (X_STEP); X_STEP++; // causes error var x : Number; x = 100; ... x = x + X_LOC;
In JavaFX, there are two ways of declaring a variable:
def
Thedef
keyword is used to declare and assign constant values. Once a variable is declared with thedef
keyword and assigned a value, it is not allowed be reassigned a new value.var
Thevar
keyword declares variables which are able to be updated at any point after their declaration.
All variables must have an associated type. The type can be declared explicitly or be automatically coerced by the compiler. Unlike Java (similar to ActionScript and Scala), the type of the variable follows the variable's name separated by a colon.
var location:String;
The following code specifies the type (class) that the variable will receive at runtime:
var location:String; location = "New York";
The compiler also supports a short-hand notation that combines declaration and initialization.
var location:String = "New York";
In this format, the type is left out of the declaration. The compiler automatically converts the variable to the proper type based on the assignment.
var location; location = "New York";
Variable location will automatically receive a type of String during compilation because the first assignment is a string literal.
Or, the short-hand version:
var location = "New York";
Similar to other languages, JavaFX supports a complete set of primitive types as listed:
:String
this type represents a collection of characters contained within within quotes (double or single, see following). Unlike Java, the default value for String is empty ("").
"The quick brown fox jumps over the lazy dog" or 'The quick brown fox jumps over the lazy dog'
:Number
this is a numeric type that represents all numbers with decimal points. It is backed by the 64-bit double precision floating point Java type. The default value of Number is 0.0
.
0.01234 100.0 1.24e12
:Integer
this is a numeric type that represents all integral numbers. It is backed by the 32-bit integer Java type. The default value of an Integer is 0
.
-44 7 0 0xFF
:Boolean
as the name implies, this type represents the binary value of either true
or false
.
:Duration
this type represent a unit of time. You will encounter its use heavily in animation and other instances where temporal values are needed. The supported units include ms, s, m
, and h
for millisecond, second, minute, and hour respectively.
12ms 4s 12h 0.5m
:Void
this type indicates that an expression or a function returns no value. Literal representation of Void
is null
.
Variables can have three distinct scopes, which implicitly indicates the access level of the variable when it is being used.
Script variables are defined at any point within the JavaFX script file outside of any code block (including class definition). When a script-level variable is declared, by default it is globally visible within the script and is not accessible from outside the script (without additional access modifiers).