Java Reference
In-Depth Information
catch (ScriptException e) {
e.printStackTrace();
}
}
}
Engine-1 - Sum = 300
Engine-2 - Sum = 300
Engine-1 - Sum = 1100
Engine-2 - Sum = 2100
The story of the global scope
Bindings
shared by all ScriptEngines that are
created by a
ScriptEngineManager
is not over yet. It is as complex, and confusing, as
it can get! Now the focus will be on the effects of using the
setBindings()
method of
ScriptEngineManager
class and the
ScriptEngine
interface. Consider the following
snippet of code:
// Create a ScriptEngineManager and two ScriptEngines
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine1 = manager.getEngineByName("JavaScript");
ScriptEngine engine2 = manager.getEngineByName("JavaScript");
// Add two key-value pairs to the manager
manager.put("n1", 100);
manager.put("n2", 200);
Figure
3-3
shows the state of the engine manager and its engines after the
above script is executed. At this point, there is only one
Bindings
stored in the
ScriptEngineManager
and two ScriptEngines are referring to it as their global scope
Bindings
.
Figure 3-3.
Initial state of ScriptEngineManager and two ScriptEngines
Search WWH ::
Custom Search