views:

1991

answers:

4

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?

+1  A: 

You can get other constructors with getConstructor(...).

iny
+6  A: 
Class.getDeclaredConstructors(types list).newInstance(args list);
Dev er dev
+1  A: 

Class.getDeclaredConstructor(String.class).newInstance("HERESMYARG");

jsight
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