|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON JavaFX Closures in Compiled JavaFX Script
One of the very useful (and cool) features of compiled JavaFX Script will be closures
By: James L. Weaver
Nov. 22, 2007 12:00 PM
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. /* 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. LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
BREAKING JAVA NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||