aspose file tools
The moose likes Java in General and the fly likes Creating run time object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply 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: 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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Creating run time object
 
Similar Threads
access modifier for constructor ?
object Vs instance
.transferFocus()
Understanding how this code works
what exactly meant by ANYCLASS.class