| Author |
How to instanciate a class by name?
|
Vinicius Souza
Ranch Hand
Joined: May 18, 2011
Posts: 40
|
|
I need tp intanciate a class using only a string. Its possibel to do it?
|
Java is the best (I love C too) heehhh
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 23635
|
|
Class c = Class.forName("nameOfClass");
c.newInstance();
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Certs: SCEA Part 1, Part 2 & 3 & Core Spring 3, OCAJP
|
 |
Vinicius Souza
Ranch Hand
Joined: May 18, 2011
Posts: 40
|
|
Jeanne Boyarsky wrote:Class c = Class.forName("nameOfClass");
c.newInstance();
Thanks a lot! Its solved my problem
Best regards,
Vinicius
|
 |
Vinicius Souza
Ranch Hand
Joined: May 18, 2011
Posts: 40
|
|
Jeanne, its means that, the class have to be an empty constructor ? And if not, its will trows an exception?
Thanks in advance!
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 3212
|
|
|
I think we need some context here. What are you trying to achieve? (Note: Not how you're trying to do it, we already have some idea of that.)
|
luck, db
There are no new questions, but there may be new answers.
|
 |
 |
|
|
subject: How to instanciate a class by name?
|
|
|