Saturday, October 25, 2025
HomeLanguagesJavaSimpleScriptContext removeAttribute() method in Java with Examples

SimpleScriptContext removeAttribute() method in Java with Examples

The removeAttribute() method of a SimpleScriptContext class is used to remove an attribute in a given scope and the name of attribute and scope are passed as parameters.

Syntax:

public Object removeAttribute(String name, int scope)

Parameters: This method accepts two parameters:

  • name which is the Name of the attribute and
  • scope which is the scope in which to remove the attribute.

Return value: This method returns removed value.

Exceptions: This method throws following Exceptions:

  • NullPointerException: if the name is null.
  • IllegalArgumentException: if the name is empty or if the scope is invalid.

Below programs illustrate the SimpleScriptContext.removeAttribute() method:
Program 1:




// Java program to demonstrate
// SimpleScriptContext.removeAttribute() 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);
  
        // remove using removeAttribute()
        Object removedObject
            = simple.removeAttribute(
                "name",
                ScriptContext.ENGINE_SCOPE);
  
        // print
        System.out.println("Removed Object :"
                           + removedObject);
    }
}


Output:

Removed Object :Value

Program 2:




// Java program to demonstrate
// SimpleScriptContext.removeAttribute() 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.ENGINE_SCOPE);
        simple.setAttribute("Team3", "Nepal",
                            ScriptContext.GLOBAL_SCOPE);
  
        // remove using removeAttribute()
        Object remove1
            = simple.removeAttribute(
                "Team1",
                ScriptContext.ENGINE_SCOPE);
        Object remove2
            = simple.removeAttribute(
                "Team2",
                ScriptContext.ENGINE_SCOPE);
  
        // print scopes of different teams
        System.out.println("Removed : " + remove1);
        System.out.println("Removed : " + remove2);
    }
}


Output:

Removed : India
Removed : Japan

References: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleScriptContext.html#removeAttribute(java.lang.String, int)

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS