Java Reference
In-Depth Information
bind recalculation because the value of
d
changed from 7 to 10. As part of the
recalculation,
c
does not change. Its value is stored and re-retrieved for the sake
of that recalculation. For the third line of output, even though
b
changes, the
bound variable
total
is not recalculated because neither
c
nor
d
changed.
In the preceding example, you may have noticed that the variable
c
is assigned
the product of variables
a
and
b
. Yet when either component of
c
changes it does
not cause
total
to be recalculated. It's quite possible that the author really
meant to have
total
updated whenever
a
,
b
, or
c
change. This can be accom-
plished by replacing
c
's declaration from
var c = a * b;
to
var c =
bind
a * b;
Now whenever
a
or
b
change,
c
will be updated, which in turn causes
total
to
be recalculated too. This cascading of bound variables is a powerful concept and is
one that you'll likely encounter often when examining JavaFX code. Re-running
our example with the modified statement yields
total=19
total=22
total=25
Binding variables to conditional expressions of the form
var v = bind if (
conditionalExpression
)
expr1
else
expr2
produces a change in which branch of the
if
-
else
statement,
expr1
or
expr2
,
gets evaluated when
conditionalExpression
changes. Again, resorting to sam-
ple code
var a = 1;
var b = 2;
var max = bind if (a > b) a else b;
println("max = {max}");
a = 3;
println("max = {max}");
b = 4;
println("max = {max}");
Search WWH ::
Custom Search