Skip to content

Overriding Java Methods

When instantiating a Java class in a script, Rhino accepts a JavaScript object literal immediately after the constructor call to override one or more methods on the instance:

var instance = new MyClass() {
    myMethod: function() {
        return "MyNewValue";
    },
    myMethodWithParameter: function(parameter) {
        return "MyNewValue" + parameter;
    }
};

Each entry in the override block names a method on the Java class and provides a JavaScript function to call in place of the Java implementation. The override applies wherever the instance's method is invoked, including Java code that holds a reference to it. See Overriding Java Methods in a Script in the JavaScript tutorial for a walkthrough.

See Common Scripting Pitfalls for Rhino/JavaScript and QIE-function traps that come up most often in custom scripts.