Java Reference
In-Depth Information
Let's create a new
Bindings
and set it as the
Bindings
for the
ScriptEngineManager
using its
setBindings()
method, like so:
// Create a Bindings, add two key-value pairs to it, and set it as the new
// Bindings for the manager
Bindings newGlobal = new SimpleBindings();
newGlobal.put("n3", 300);
newGlobal.put("n4", 400);
manager.setBindings(newGlobal);
Figure
3-4
shows the state of the
ScriptEngineManager
and two
ScriptEngine
s after
the code is executed. Notice that the
ScriptEngineManager
has a new
Bindings
and the
two
ScriptEngine
s are still referring to the old
Bindings
as their global scope
Bindings
.
Figure 3-4.
State of ScriptEngineManager and two ScriptEngines after a new Bindings is
set to the ScriptEngineManager
At this point, any changes made to the
Bindings
of the
ScriptEngineManager
will
not be reflected in the global scope
Bindings
of the two
ScriptEngine
s. You can still
make changes to the
Bindings
shared by the two
ScriptEngine
s and both
ScriptEngine
s
will see the changes made by either of them.
Let's create a new
ScriptEngine
, as shown:
// Create a new ScriptEngine
ScriptEngine engine3 = manager.getEngineByName("JavaScript");
Recall that a
ScriptEngine
gets a global scope
Bindings
at the time it is created
and that
Bindings
is the same as the
Bindings
of the
ScriptEngineManager
. The state of
the
ScriptEngineManager
and three ScriptEngines, after this statement is executed, are
shown in Figure
3-5
.
Search WWH ::
Custom Search