This week's book giveaway is in the Security forum.
We're giving away four copies of Securing DevOps and have Julien Vehent on-line!
See this thread for details.
Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to instanciate a class by name?  RSS feed

 
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need tp intanciate a class using only a string. Its possibel to do it?
 
author & internet detective
Marshal
Posts: 37898
596
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class c = Class.forName("nameOfClass");
c.newInstance();
 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Class c = Class.forName("nameOfClass");
c.newInstance();


Thanks a lot! Its solved my problem
Best regards,
Vinicius
 
Vinicius Souza
Ranch Hand
Posts: 70
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne, its means that, the class have to be an empty constructor ? And if not, its will trows an exception?
Thanks in advance!
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!