JavaFX: Create Properties More Efficiently

By Doug Lowe

The advanced capabilities of JavaFX properties do not come without a cost. Specifically, instantiating a property object takes more memory and processing time than creating a simple field-based property. And in many classes, the advanced capabilities of a JavaFX property object are only occasionally needed. This, instantiating property objects for every property in a class whether the object is needed or not, is wasteful.

Here’s a technique for creating properties in which the property objects themselves are not instantiated until the property accessor itself is called. That way, the property object is not created unless it is actually needed. Here are the details of this technique:

  1. Declare a private field to hold the data represented by the property.

    For example, for a string property, you create a String variable. For the variable name, use the name of the property prefixed by an underscore, like this:

    private final String _firstName = ";
  2. Create, but do not instantiate, a private variable to represent the property object.

    In other words, declare the variable but do not call the class constructor:

    private final SimpleStringProperty firstName;
  3. Create the getter.

    In the getter, use an if statement to determine whether the property object exists. If it does, return the value from the property. If it doesn’t, return the value of the private field. For example:

    public final String getFirstName()
    {
        if (firstName == null)
            return _firstName;
        else
            return firstName.get();
    }
  4. Create the setter.

    Use the same technique in the setter:

    public final void setFirstName(String value)
    {
        if (firstName == null)
            _firstName = value;
        else
            firstName.set(value);
    }
  5. Create the property accessor.

    In this method, first check whether the property object exists and create the object if it does not exist. Use the value of the private field as the initial value of the property. Then, return the object:

public final StringProperty firstNameProperty()
{if (firstName == null)
        firstName = new SimpleStringProperty(
            this, "firstName", _firstName);
   return firstName;
}

Here’s what it looks like put together in a class named Customer:

Public class Customer
{
    private final String _firstName = ";
    private final SimpleStringProperty firstName;
    public final String getFirstName()
    {
        if (firstName == null)
            return _firstName;
        else
            return firstName.get();
    }
    public final void setFirstName(String value)
    {
        if (firstName == null)
            _firstName = value;
        else
            firstName.set(value);
    }
    public final StringProperty firstNameProperty()
    {          if (firstName == null)
            firstName = new SimpleStringProperty(
                this, "firstName", _firstName);
       return firstName;
    }
}