JavaFX is an object-oriented scripting language. As such, object types, represented as classes, are part of the basic constructs of the language. This section shows how to declare, initialize, and use JavaFX classes.
If you have used other scripting languages such as ActionScript, JavaScript, Python, or PHP, the concepts presented in this section should be familiar. If you have no idea what a class is or what it should be, just remember this: a class is code that represents a logical entity (tree, person, organization, and so on) that you can manipulate programmatically or while using your application. A class usually exposes properties and operations to access the state or behavior of the class.
Let's assume we are building an application for a dealership. You may have a class called Vehicle
to represent cars and other type of vehicles processed in the application. The next code example creates the Vehicle
class. Refer to ch01/source-code/src/javafx/Vehicle.fx
for full listing of the code presented here.
1. Open your favorite text editor (or fire up your favorite IDE).
2. Type the following class declaration.
class Vehicle { var make; var model; var color; var year; function drive () : Void { println("You are driving a " "{year} {color} {make} {model}!") } }
3. Once your class is properly declared, it is now ready to be used. To use the class, add the following (highlighted code) to the file:
class Vehicle { ... } var vehicle = Vehicle { year:2010 color: "Grey" make:"Mini" model:"Cooper" }; vehicle.drive();
4. Save the file as
Vehicle.fx
. Now, from the command-line, compile it with$> javafxc Vehicle.fx
Note
If you are using an IDE, you can simply right, click on the file to run it.
When the code executes, you should see:
$> You are driving a 2010 Grey Mini Cooper!
The previous snippet shows how to declare a class in JavaFX. Albeit a simple class, it shows the basic structure of a JavaFX class. It has properties represented by variables declarations:
var make; var model; var color; var year;
and it has a function:
function drive () : Void { println("You are driving a " "{year} {color} {make} {model}!") }
which can update the properties and/or modify the behavior (for details on JavaFX functions, see the recipe Creating and Using JavaFX functions). In this example, when the function is invoked on a vehicle
object, it causes the object to display information about the vehicle on the console prompt.
Another aspect of JavaFX class usage is object declaration. JavaFX supports object literal declaration to initialize a new instance of the class. This format lets developers declaratively create a new instance of a class using the class's literal representation and pass in property literal values directly into the initialization block to the object's named public properties.
var vehicle = Vehicle { year:2010 color: "Grey" make:"Mini" model:"Cooper" };
The previous snippet declares variable vehicle
and assigns to it a new instance of the Vehicle class with year = 2010, color = Grey, make = Mini
, and model = Cooper
. The values that are passed in the literal block overwrite the default values of the named public properties.
JavaFX class definition mechanism does not support a constructor as in languages such as Java and C#. However, to allow developers to hook into the life cycle of the object's instance creation phase, JavaFX exposes a specialized code block called init{}
to let developers provide custom code which is executed during object initialization.
Code in the init
block is executed as one of the final steps of object creation after properties declared in the object literal are initialized. Developers can use this facility to initialize values and initialize resources that the new object will need. To illustrate how this works, the previous code snippet has been modified with an init
block. You can get the full listing of the code at ch01/source-code/src/javafx/Vehicle2.fx
.
class Vehicle { ... init { color = "Black"; } function drive () : Void { println("You are driving a " "{year} {color} {make} {model}!"); } } var vehicle = Vehicle { year:2010 make:"Mini" model:"Cooper" }; vehicle.drive();
Notice that the object literal declaration of object vehicle
no longer includes the color declaration. Nevertheless, the value of property color
will be initialized to Black
in the init{}
code block during the object's initialization.
When you run the application, it should display:
You are driving a 2010 Black Mini Cooper!