wood burning stoves 2.0*
The moose likes Java in General and the fly likes Creating run time object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating run time object" Watch "Creating run time object" New topic
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: 19685
    
  20

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 - OCEEJBD 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating run time object