| Author |
Creating run time object
|
angelin precilla
Ranch Hand
Joined: Sep 11, 2007
Posts: 33
|
|
Only during runtime , i know the class for which i need to create the object.How to generate objects for that class in java? I did like Class CN= Class.forName("nxo.ebs.newproject2.service.DropdownService"); Object o = CN.newInstance(); -----> java.lang.InstantiationException
|
 |
bart zagers
Ranch Hand
Joined: Feb 05, 2003
Posts: 234
|
|
From the Class javadoc: InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason. Most probably your class does not have a default (no-arg) constructor.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
If you don't have a constructor without parameters, you need to retrieve the right constructor using Class' getConstructor() method, passing the right parameters. Then use that Constructor object to create the new instance, again passing the right parameters.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
angelin precilla
Ranch Hand
Joined: Sep 11, 2007
Posts: 33
|
|
|
Thanks for your comments.
|
 |
angelin precilla
Ranch Hand
Joined: Sep 11, 2007
Posts: 33
|
|
|
Thanks for your comments.
|
 |
 |
|
|
subject: Creating run time object
|
|
|