YOUR FEEDBACK
Jeremy Geelan wrote: In response to inquiries and suggestions from readers this lexicon has recently...


2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


Closures in Compiled JavaFX Script
One of the very useful (and cool) features of compiled JavaFX Script will be closures

From Jim Weaver's Learn JavaFX Weblog

One of the very useful (and cool) features of compiled JavaFX Script will be closures.  In a nutshell, JavaFX Script closures provide the ability to define a function within another function with the inner function having access to the local variables of the outer function.  This feature is enabled by the fact that in compiled JavaFX Script, functions are first-class objects, which provides the ability to assign functions to variables and to pass functions as arguments to other functions.  You can read more about closures in this Wikipedia article.

Closures are often used to implement callback functions, many times handling UI events.  Since the UI part of compiled JavaFX Script isn't ready for prime-time yet, I'd like to show you a callback example in which a function is called when a timer expires.  You can run this example by downloading and building the JavaFX Compiler in its current state from the Subversion repository in the OpenJFX Compiler Project

Here's the source code for the example:

/*
*  ClosureExample.fx - Demonstrates closures, and that
* functions are first-class
*  objects in compiled JavaFX Script.
*
*  Uses JavaFX Script compiled version
*  Developed 2007 by James L. Weaver
* (jim.weaver at lat-inc dot com)
*/

import java.lang.System;
import java.lang.Thread;

public class ClosureExample {

  function startTimer(millis:Integer,
wakeUp:function():Void):Void {
    Thread.currentThread().sleep(millis);
    wakeUp();
  }
 
  function setAlarm(sleepTime:Integer):Void {
    var message = "Time to wake up!";
    var wakeUpFunc =
      function() {
        System.out.println(message);
        System.out.println("You slept
for {sleepTime} milliseconds");
      };
    startTimer(sleepTime, wakeUpFunc);
   
  }
}

var example =
  ClosureExample {
  };
 
example.setAlarm(3000);

Walking Through the Closure Related Features of this Example

After creating an instance of the ClosureExample class, and calling its setAlarm() function, you'll notice that an anonymous function is defined and assigned to a variable named wakeUpFunc.  After that, the startTimer() function is invoked, passing the desired duration of the timer, as well as the wakeUpFunc reference.

Within the startTimer() function, the sleep() method of the Java Thread class is called, which causes the current thread to sleep for the desired number of milliseconds.  This, by the way, demonstrates JavaFX Script's ability to leverage the power of Java classes (see the Putting My CTO Hat On post).  After the thread wakes up, the callback function that was passed into the startTimer() function is invoked, which performs the closure.

The net effect is that the callback function accesses and prints the values of local variables of the setAlarm() function.  The closure feature allows access to these variables from within the scope of the callback function, even though they're not local to the callback function.

About James L. Weaver
James L. (Jim) Weaver is founder and president of jMentor, formed in 2000 to provide Java programming-related training to companies and individuals. He has served as a system architect and developer for over 25 years, specializing in leading-edge software development. His specialties include Java, object-oriented, and web-based technologies. He has authored books on the Java programming language, including most recently JavaFX Script, published by Apress.

LATEST JAVA STORIES & POSTS
What's the key to team and individual developer productivity in maintaining and extending a large application? Let’s start by making the following assertions: A developer's knowledge of an application code base is likely the single biggest factor of individual productivity. Cor...
An applet, a Java program that runs in a browser, often has to access the client resources. However, the security manager prevents an applet from accessing client resources. To access client resources, the applet has to have the proper permission. With this permission the applet ...
Three-letter acronyms (TLAs) are hardly new in Information Technology: EAI, ESB, SOA, BPM, BAM, ETL, MDM; the list goes on and on. This article is about yet another three-letter acronym, EDA, which stands for Event-Driven Architecture. EDA is not a brand new technology, but rathe...
Furthering its dedication to providing Java developers productivity with choice, Oracle announced the Oracle Enterprise Pack for Eclipse, a new component of Oracle Fusion Middleware. This release marks the first free Eclipse 3.4 environment to support Oracle WebLogic Server 10g R...
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted...
Red Hat CTO Brian Stevens, Citrix CTO Simon Crosby, Egenera CTO Pete Manca, Allen Stewart, Group Manager, Windows Virtualization at Microsoft, and Brian Duckering, Sr. Director of Products and Alliances at Symantec were the top industry executives who joined Jeremy Geelan in the ...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

SPONSORED BY INFRAGISTICS
There are many forces that influence technological evolution. After a decade of building enterprise ...
2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
In every field of design one of the first things students do is learn from the work of others. They ...
Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
ADS BY GOOGLE
BREAKING JAVA NEWS

SpringSource, a leading provider of infrastructure software and the company behind ...