aspose file tools*
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
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: 45

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: 30774
    
156

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Vinicius Souza
Ranch Hand

Joined: May 18, 2011
Posts: 45

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: 45

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: 4642
    
    5

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?