Thursday, February 12, 2026
HomeLanguagesJavaSimpleScriptContext setBindings() method in Java with Examples

SimpleScriptContext setBindings() method in Java with Examples

The setBindings() method of a SimpleScriptContext class is used to set a Bindings of attributes for the given scope where Bindings and scope are passed as parameters to the method. If the scope is ENGINE_SCOPE the given Bindings replaces the engineScope field. If the scope is GLOBAL_SCOPE the given Bindings replaces the globalScope field.

Syntax:

public void setBindings(Bindings bindings,
                        int scope)

Parameters: This method accepts two-parameters:

  • bindings which is the Bindings of attributes to set and
  • scope which is the scope in which to set the attribute.

Return value: This method returns nothing.

Exceptions: This method throws following Exceptions:

  • NullPointerException: if the value of scope is ENGINE_SCOPE and the specified Bindings is null.
  • IllegalArgumentException: if the scope is invalid.

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




// Java program to demonstrate
// SimpleScriptContext.setBindings() method
  
import javax.script.ScriptContext;
import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create SimpleScriptContext object
        SimpleScriptContext simple
            = new SimpleScriptContext();
  
        // create Bindings
        SimpleBindings bindings
            = new SimpleBindings();
  
        // add some key-value to bindings
        bindings.put("name1", "Value1");
  
        // add bindings to SimpleScriptContext using
        // setBindings() Method
        simple.setBindings(
            bindings,
            ScriptContext.ENGINE_SCOPE);
  
        // print
        System.out.println(
            "name1:"
            + simple.getAttribute("name1"));
    }
}


Output:

name1:Value1

Program 2:




// Java program to demonstrate
// SimpleScriptContext.setBindings() method
  
import javax.script.ScriptContext;
import javax.script.SimpleBindings;
import javax.script.SimpleScriptContext;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create SimpleScriptContext object
        SimpleScriptContext simple
            = new SimpleScriptContext();
  
        // create Bindings
        SimpleBindings bindings
            = new SimpleBindings();
  
        // add some key-value to bindings
        bindings.put("Team1", "India");
        bindings.put("Team2", "Japan");
        bindings.put("Team3", "Nepal");
  
        // add bindings to SimpleScriptContext using
        // setBindings() Method
        simple.setBindings(
            bindings,
            ScriptContext.ENGINE_SCOPE);
  
        // print
        System.out.println("Team1:"
                           + simple.getAttribute("Team1"));
        System.out.println("Team2:"
                           + simple.getAttribute("Team2"));
        System.out.println("Team3:"
                           + simple.getAttribute("Team3"));
    }
}


Output:

Team1:India
Team2:Japan
Team3:Nepal

References: https://docs.oracle.com/javase/10/docs/api/javax/script/SimpleScriptContext.html#setBindings(javax.script.Bindings, int)

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32497 POSTS0 COMMENTS
Milvus
128 POSTS0 COMMENTS
Nango Kala
6869 POSTS0 COMMENTS
Nicole Veronica
11996 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12090 POSTS0 COMMENTS
Shaida Kate Naidoo
7009 POSTS0 COMMENTS
Ted Musemwa
7246 POSTS0 COMMENTS
Thapelo Manthata
6958 POSTS0 COMMENTS
Umr Jansen
6947 POSTS0 COMMENTS