Thursday, 15 March 2007

Reflection Reflecting

If you create a class object, for example from a class loader's loadClass method, then you might like to use the newInstance method to get a new instance of your class.

Why not, right?

Well, just beware.

Make sure that the class you loaded in has a constructor which takes no arguments.

If (as happened to me) the first time you tried this was on a class that had only one constructor, and that constructor took a String argument, then what you got back, when trying to cast the class to the sort of interface I wanted was an InstantiationException/Error.

No comments: