Java Reference
In-Depth Information
are unidirectional in nature unless they are specified as bidirectional. This is
achieved by including the with inverse keywords as part of the bind expres-
sion. We'll discuss bind with inverse later on in this chapter; suffice it to say, it is
traditionally used far less often than the default bind behavior.
In order to contrast between a bound variable and regularly defined variable, a
third variable z is introduced. It too is assigned the value of x , but as it is
unbound, its value will not change as x changes. Compiling and running this
chunk of code produces output as follows:
x=10, y=10, z=10
x=20, y=20, z=10
Binding to Instance Variables
Variables inside classes can also be bound. To demonstrate, let's make a few
modifications to the preceding example code. Instead of having y and z variables
defined at the script level, this time they're placed inside a class called myClass .
In this case, an instance of myClass has to be created, where within the object lit-
eral the binding of instance variable y takes place. Line 6 in the following listing
creates the binding.
var x : Integer = 10;
class myClass {
var y : Integer;
var z : Integer;
};
var m = myClass { y: bind x, z: x };
println("x={x}, m.y={m.y}, m.z={m.z}");
x = 20;
println("x={x}, m.y={m.y}, m.z={m.z}");
Not unlike the previous example, an additional instance variable, z , is also
defined but unbound to demonstrate that its value does not change as the value of
x does. Compiling and running this code block produces this output:
x=10, m.y=10, m.z=10
x=20, m.y=20, m.z=10
A best effort has been made to assure that the included code blocks contained in
this chapter can be compiled and executed in a standalone fashion. When dealing
with class definitions, more than likely you'll want to declare them as public .
For our examples, we declare the classes without preceding them with the public
 
Search WWH ::




Custom Search