File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30551
    
152

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: 4547
    
    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.
 
Consider Paul's rocket mass heater.
 
subject: How to instanciate a class by name?