I would like to use Class.newInstance()
but the class I am instantiating does not have a nullary constructor. Therefore I need to be able to pass in constructor arguments. Is there a way to do this?
views:
1991answers:
4
+6
A:
Class.getDeclaredConstructors(types list).newInstance(args list);
Dev er dev
2008-10-24 17:51:53
+1
A:
Class.getDeclaredConstructor(String.class).newInstance("HERESMYARG");
jsight
2008-10-24 17:52:26
A:
Do not use Class.newInstance()
; see this thread: Why is Class.newInstance() evil?
Like other answers say, use Constructor.newInstance()
instead.
Chris Jester-Young
2008-10-24 20:43:51