• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forname

 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to understand this by my own... I couldn't. then I tried:
JFrame frame=(JFrame)Class.forName("javax.swing.JFrame");
then it did not work;
after I tried:
JFrame frame=(JFrame)Class.forName("javax.swing.JFrame").newInstance();
after that I noticed that it was the same as
JFrame frame=new JFrame();
funny!!! but what does Class.forName do???
Sorry for the english... I'm from another country and I think my english is getting worse....
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say that, as an example, you read in the name of a class from a configuration file. Your program has no way of knowing what the class name is.
Through the use of Class.forName() you can create an instance of the class.
A good example of this is JSP custom tags. The class implementing the tag is named in the TLD file (an XML descriptor of the tag library), and the JSP container creates instances of the class as needed.
hth,
bear
[ November 07, 2002: Message edited by: Bear Bibeault ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if all your questions were answered. Just in case...
JFrame frame=(JFrame)Class.forName("javax.swing.JFrame");
This didn't compile, because Class.forName() returns a Class object, not a JFrame object. But you can create a JFrame instance from the Class using the newInstance() method of class, as you did in your second attempt.
[ November 07, 2002: Message edited by: Jim Yingst ]
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks... I think that I did not understand because many books just order you to use class.forName to load a driver... they do not say what it does!!!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic