This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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....
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 ]
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 ]
"I'm not back." - Bill Harding, Twister
Joined: Nov 07, 2002
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!!!