A static field or method is one that is accessible via the class itself without the need for an instance of the class. Constants, such as the number of months in a year, or methods that perform calculations that don’t require access to any instance fields are examples of cases where static fields and methods should be used. Java has had static fields and methods since its inception. JavaFX used to have static fields and functions, but no longer does. They have been replaced with script level variables and script level functions. A script level variable or function is declared outside of the class definition and behaves pretty much like static fields and methods in Java.

Here is an example of static fields and methods in Java in a file called TimeTools.java:

Notice that MONTHS_IN_YEAR and the yearsToMonths() function are both declared outside of the TimeTools class definition. If they were inside the class then an instance of TimeTools would be required in any place where they were going to be accessed.

Again, the TimeTools prefix is only needed from outside of this file.

So while there’s no longer a static keyword in JavaFX, the equivalent functionality can be achieved through script level variables and functions. Just remember to pull your fields and functions outside of the class definition and then use them like you did in Java.

I will be speaking at JavaOne in San Francisco on Tuesday, May 6, 2008 from 12:10pm – 1:10pm. Below is the abstract for my session.

Joshua Smith

TS-5138 – The JavaFX™ Platform: Sexy Interfaces For Mere Mortals

JavaFX™ Script is a compiled, declarative scripting language that runs on the desktop, in the web browser and on mobile devices. It’s tightly couple with Java™ technology for core application functionality, heavy lifting and libraries. It makes special effects and GUI transitions accessible to people who aren’t graphic artists or know the intricacies of overriding Swing painters and working with the Java 2D™ API affine transformations. JavaFX Script is a perfect user-interface compliment for Java applications.

Throughout the session we’ll look at some powerful language idioms that make special effects, interface transitions and vector graphics accessible to programmers who have not chosen to specialize in advanced Swing and Java 2D. The bind keyword connects model and view and handles two-way propagation of changes. Sequences are a data structure similar to arrays, but allow for things like “all numbers between 1 and 100 that are divisible by 3”. Key-Frame animation is used to “animate” the value of variables over time. A very dynamic user interface is possible when you bind things like position, opacity and radius to these values. Triggers allow functionality that is similar to property change listeners. Transform and morphing features make movement, scaling and shape changes possible in one or two lines of code.

The majority of the session will be spent looking at visually rich examples of what is possible in Java FX Script. We’ll look at examples of image transitions, fade-ins, fade-outs, morphing and a variety of effects. Full source code of all examples will be provided. Come expecting to see lots of code. The techniques demonstrated will be directly applicable to both consumer and business applications.

No prior knowledge of JavaFX Script is required. It is expected that the attendee will have a working knowledge of Java as there will be many comparisons made between how things are done in Java vs. how they are done in JavaFX Script. Basic knowledge of Swing is helpful, but by no means necessary.

Java FX Script is a compiled, declarative scripting language that runs on the desktop, in the web browser and on mobile devices. It’s tightly couple with Java for core application functionality, heavy lifting and libraries. It makes special effects and GUI transitions accessible to people who aren’t graphic artists or know the intricacies of overriding Swing painters and working with Java 2D affine transformations. Java FX Script is a perfect user-interface compliment for Java applications.