dcsimg
How to store a class name in a variable and instantiate the object using that variable?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sekar_Balashanmugam
Posted On:   Tuesday, April 19, 2005 02:08 PM

I'm trying to store a class name (i.e., "com.xyz.myclass") in a String variable and would like to instantiate it. Is there a way to acheive this in Java?

Re: How to store a class name in a variable and instantiate the object using that variable?

Posted By:   Jani_Kaarela  
Posted On:   Wednesday, April 20, 2005 12:05 AM

Yes. Get the corresponding Class by using Class.forName(). Then instantiate it using Class.newInstance() or use reflection, if your class needs constructor params. See java.lang.Class and the whole java.lang.reflection package.
About | Sitemap | Contact