Java FX class, function, block expression and object 

Creating a Class:
Creating a class in JavaFX is similar to creating a class in Java.  The class declaration should start with an access modifier followed by keyword ‘class‘ and the desired classname. If no access modifier is specified JavaFX assumes the access modifier to be default access i.e. the class cannot be accessed from other FX scripts and can only be accessed in the same script where it is declared. It is similar to private access modifier in Java. Also, class members can be only  var, def, function, init, or postinit.

More information about access modifier. (

Let’s start with an example:

public class Name
var title: String;
var firstName: String;
var middleInitial:  String;
var lastName: String;
var YOB: Integer;

public var age =
var currentYear = 2011;
var currentAge = currentYear  - YOB; //Last expression

public var fullName =  "{firstName}  {middleInitial}  {lastName}";

public function getTitle(): String
return this.title;

public function setTitle(title:String):Void
this.title = title;

//Instantiating an object of class Name and assigning it to variable name1.
var name1 = Name
title:  "Mr";
firstName: "Henry";
middleInitial: "G";
lastName: "Thomas";
YOB: 1984;

In the above example, the public access modifier makes the class visible and accessible to other script files. The name of the class is ‘Name’. The class contains four default access string variables title, firstName, middleInitial, and lastName and one default access Integer YOB. Since no access modifier is specified for these variables, they have default access as their access modifier. So, these variables are accessible within the class ‘Name’ only.  Suppose you create an object of the class ‘Name’ in some other script then these variables cannot be accessed from that script.

Block Expression:
A block expression starts  and ends with curly braces (starts with ‘{‘ and ends with ‘}’). It contains a list of declarations and expressions. The block expression returns the value of the last expression or is a Void type if no expression exists within the block. The variable ‘age’ is assigned a block expression.  The last expression within the block is currentAge which is returned and then is assigned to variable ‘age’. So the value of age is whatever the value of currentAge is calculated from the expression. Notice that the variable ‘age’ has a public access modifier. So, this variable can be accessed wherever object of the class is instantiated. Also the variable fullName is assigned a string of block expressions. {firstName} returns the value of class variable firstName, {middleInitial} returns the value of class variable middleInitial and {lastName} returns the value of class variable lastName. Keep in mind that ‘var’ declarations and ‘def’ declarations  are expressions and hence the value is returned by the block expression.

Declaring Function:
Function declarations start with access modifier followed by the keyword function and then the function name. The return type is followed by a colon:‘ after the function name. getTitle() and setTitle() are two functions. getTitle()  has a public access modifier and has no function parameters and returns the type String.  setTitle() also has a public access modifier with a string parameter ‘title’ and has a return type of Void i.e. nothing is returned. Multiple function parameters can be specified by separating each with a comma ‘,’.
public function test (arg1:String, arg2:Integer):Integer
…..some expression
This function has two parameters arg1 which is a string and arg2 which is an integer and the return type of the function is  integer.

Instantiating an Object:
The variable name1 is assigned an object of the class Name.  The class members are initialized within the curly braces. So name1 object of class name has “Mr” as value for title, “Henry” as value for firstName, “G” as value for middle initial, “Thomas” as value for lastName, and 1984 as value for YOB.
name1.getTitle() will return string value  “Mr” and name1.setTitle(“Jr”) will set the value of title to “Jr” in name1. Keep in mind that the object is instantiated in the same FXscript file and hence the default access members(title, firstName, middleInitial, lastName) are directly accessible. If the object was instantiated in some other FXscript then the default access modifiers would not be accessible and only the declarations with public access modifiers would be accessible. For example the variable ‘title’ would not be accessible but the  variable ‘fullName’ would be.