Probably ill-conceived and I probably won't ever need this. But just noting this down somewhere if I ever do...
Let's say you have a module or script or something that maintains state, and you have a single script-engine instance. You want each custom script to get its own instance of this module (i.e., multiple scripts executing on the engine must not step over each other's state). How do you do this? You precompile then when it's time to execute your custom script, you first execute the precompiled script. Then you copy everything it exposes into your custom script's context. Snippet:
//Precompile the modules and put them in a map
private void compileModule(String name, String source) {
try {
this.engine.put(ScriptEngine.FILENAME, String.format("<%s>", name));
this.compiledModules.put(name, ((Compilable) this.engine).compile(source));