• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating run time object

 
angelin precilla
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
angelin precilla
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your comments.
 
angelin precilla
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your comments.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic