This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes How to instanciate a class by name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "How to instanciate a class by name?" Watch "How to instanciate a class by name?" New topic
Author

How to instanciate a class by name?

Vinicius Souza
Ranch Hand

Joined: May 18, 2011
Posts: 70
    
    1

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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 32609
    
215

Class c = Class.forName("nameOfClass");
c.newInstance();


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Vinicius Souza
Ranch Hand

Joined: May 18, 2011
Posts: 70
    
    1

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: 70
    
    1

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: 5041
    
  10

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to instanciate a class by name?
 
It's not a secret anymore!