SimpleScriptContext getAttributesScope() method in Java with Examples

The getAttributesScope() method of a SimpleScriptContext class is used to return the scope in which an attribute is defined and the name of attribute is passed as parameter.
Syntax:
public int getAttributesScope(String name)
Parameters: This method accepts a single parameter name which is the Name of the attribute.
Return value: This method returns lowest scope and returns -1 if no attribute with the given name is defined in any scope.
Exceptions: This method throws following Exceptions:
- NullPointerException if the name is null.
- IllegalArgumentException if the name is empty.
Below programs illustrate the SimpleScriptContext.getAttributesScope() method:
Program 1:
// Java program to demonstrate// SimpleScriptContext.getAttributesScope() method import javax.script.ScriptContext;import javax.script.SimpleScriptContext; public class GFG { public static void main(String[] args) { // create SimpleScriptContext object SimpleScriptContext simple = new SimpleScriptContext(); // add some attribute simple.setAttribute( "name", "Value", ScriptContext.ENGINE_SCOPE); // get scope using getAttributesScope() int scope = simple.getAttributesScope("name"); // print System.out.println("Scope :" + scope); }} |
Output:
Scope :100
Program 2:
// Java program to demonstrate// SimpleScriptContext.getAttributesScope() method import javax.script.ScriptContext;import javax.script.SimpleScriptContext; public class GFG { public static void main(String[] args) { // create SimpleScriptContext object SimpleScriptContext simple = new SimpleScriptContext(); // add some attribute simple.setAttribute( "Team1", "India", ScriptContext.ENGINE_SCOPE); simple.setAttribute( "Team2", "Japan", ScriptContext.GLOBAL_SCOPE); simple.setAttribute( "Team3", "Nepal", ScriptContext.GLOBAL_SCOPE); // get scope using getAttributesScope() int scope1 = simple.getAttributesScope("Team1"); int scope2 = simple.getAttributesScope("Team2"); int scope3 = simple.getAttributesScope("Team3"); // print scopes of different teams System.out.println("Scope for Team1: " + scope1); System.out.println("Scope for Team2: " + scope2); System.out.println("Scope for Team3: " + scope3); }} |
Output:
Scope for Team1: 100 Scope for Team2: -1 Scope for Team3: -1
References: https://docs.oracle.com/javase/10/docs/api/javax/script/ScriptContext.html



