Instantiating from a class which name is passed in a string.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, January 31, 2002 05:10 PM

Hi there, I am a student currently developing a project, and would like to know if there is a way, and which is it, to create an instance of a class which name is passed to us in a string. We can not know in advance the class name because it can be anyone, there are no limitations in that aspect.
Thank you in advance. Perhaps you need more details.

Re: Instantiating from a class which name is passed in a string.

Posted By:   AlessandroA_Garbagnati  
Posted On:   Friday, February 1, 2002 02:03 AM

Juan Pedro,


Take a look at java.lang.Class:


Object myObject = Class.forName("package.MyClass").newInstance();


Check the javadoc and the FAQs on here, to get further information, especially regarding error handling.

About | Sitemap | Contact