What Are JavaFX Properties?

By Doug Lowe

Java has no built-in features for implementing properties … at least, not until now. In object-oriented programming parlance, a property is a value that represents the state of an instantiated object which can be retrieved and in some cases set by users of the object.

In some object-oriented programming languages, such as C#, the concept of properties is built in to the language. Alas, such is not the case with Java.

Prior to Java 8, Java developers usually followed the pattern of using property getters and setters to retrieve and set property values. A property getter is a public method that retrieves the value of a property, and a setter is a public method that sets the value of a property.

For example, suppose you’re creating a class that represents a customer, and each customer is identified by a unique customer number. You might store the customer number internally as a private field named customerNumber. Then, you’d provide a public method named getCustomerNumber to return the customer’s number and another public method named setCustomerNumber to set the customer number. The resulting code would look like this:

class Customer
    private int customerNumber;
    public int getCustomerNumber()
        return customerNumber;
    public void setCustomerNumber(int value)
        customerNumber = value;

This pattern of using getter and setter methods has a name: the accessor pattern. (No, this has nothing to do with property taxes. Property taxes are determined by an assessor, not an accessor.)

JavaFX 8 introduces a new scheme for implementing properties, which dutifully follows the accessor pattern. As a result, JavaFX classes that implement properties must provide a getter and a setter that returns and sets the value of the properties (unless the property is read-only, in which case only a get method is required).

However, instead of using a simple field to represent the value of the property, JavaFX uses special property classes to represent properties. These classes encapsulate the value of a property in an object that provides the new whiz-bang features that enable you to listen for changes in the property value or bind properties together.

When JavaFX properties are used, the getter and setter methods return the value that’s encapsulated by the property object. In addition to the getter and setter methods, JavaFX properties introduce a third method which returns the actual property itself. This allows users of the property to directly access the property object, which in turn lets them access those new whiz-bang features.

Naming conventions are an essential aspect of using JavaFX properties correctly. Every property has a name, which by convention begins with a lowercase letter. For example, a property that represents a person’s first name might be called firstName.

The getter and setter methods are created by capitalizing the property name and prefixing it with the word get or set. The method that returns the property object is the name of the property (uncapitalized) followed by the word Property.

Thus, a class that implements a read/write property named firstName must expose three methods:




If the property is read-only, the setFirstName method would be omitted.