Java Reference
In-Depth Information
keyword. Although not ideal, this will facilitate you being able to cut, paste, and
execute these code blocks into an IDE without having to modify the code.
When Can a Variable Be Bound?
With the default bind behavior, a variable can only be bound when it is being
defined. In JavaFX, this takes place either with a var or def declaration or during
object instantiation via object literals. (The topic of binding and object literals,
being a bit more complicated, will be discussed later on in this chapter.) Endeav-
oring to bind in any other fashion will either result in a compiler or runtime
error. For example, declaring a variable first and then trying to use bind in an
assignment statement like
var x : Integer = 10;
var y : Integer;
y = bind x;
// <-- Illegal
produces the following compilation error, shortened for the sake of brevity:
JavaFX compilation
executing commandline: ...
main.fx:3: Sorry, I was trying to understand an
expression but I got confused when I saw 'bind' which is a
keyword.
y = bind x;
// <-- Illegal
Furthermore, after a variable has been bound, by default it cannot be subse-
quently reassigned. Attempting to do so will throw an AssignToBoundException
when the guilty code is actually run. As an example, the following code will
compile successfully:
var x : Integer = 10;
var y = bind x;
println("x={x}, y={y}");
y = 15; // Reassigning a bound variable?
x = 20;
println("x={x}, y={y}");
But upon execution, when the assignment of the previously bound variable is
encountered, a runtime exception is thrown:
x=10, y=10
com.sun.javafx.runtime.AssignToBoundException: Cannot
assign to bound variable
 
Search WWH ::




Custom Search