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
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Reply 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: 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.
 
IntelliJ Java IDE
 
subject: How to instanciate a class by name?
 
Threads others viewed
Doubt about diagram class
WA #1.....word association
Doubt about diagram class (realization)
can non-static inner class access any member
andrew has wii
developer file tools