Yes, it is possible
Take a look at the package "javax.tools"
You could compile and load a class at runtime.
String rogue = "package abc; "+
"public class RogueObjectWreakingHavoc extends SomeImportantClass {"+
" { System.exit(-1) }"+
"}"
File sourceFile = new File("RogueObjectWreakingHavoc.java");
FileWriter fileWriter = new FileWriter(sourceFile);
fileWriter.write(rogue);
fileWriter.close();
List<File> files = new ArrayList<File>();
files.add(sourceFile);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null,
null,
null);
compiler.getTask(null,
fileManager,
null,
null,
null,
fileManager.getJavaFileObjectsFromFiles(files))
.call();
fileManager.close();
// load using URLClassLoader...
To avoid it you most consider declare your classes as final if they are not indented to be subclasses, and make copies when needed ( instead of taking arguments just they way they are )
More information ( about how to use Java effectively ) on the book "Effective Java" by Joshua Bloch