| Author |
Class.forName
|
Glenny Dsilva
Ranch Hand
Joined: May 09, 2005
Posts: 42
|
|
Class.forName dynamically loads a class but what is the benefit of this. can anyone explain with an example
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
Hi, welcome to the ranch! Sometimes we know the name of a class at runtime but not at compile time. We can use Class.forName() to get the Class or an instance. Think of a plugin system, maybe something that adds new commands to an editor. In the configuration we might say "when the user enters this string, run this class". At runtime we can get the Class object and use that to create an instance. The configuration might say: ConvertToUpperCase = com.my.editor.macros.UpperCase At runtime we might say: Now we can add new macros that the editor author never heard of just by adding configuration. This is a slick way to "open" the editor for new functions while keeping its code "closed" for modification. Does that makes sense?
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Cesar Olavo
Greenhorn
Joined: Nov 19, 2004
Posts: 17
|
|
Hi, You can also use getClass() to keep your code easier (to maintain). For example, if you want to add logging to a dozen classes, using Jakarta commons-logging, you can cut and paste this code snippet to all of them: The alternative way would be to have a different code in each class (changing ClassOne.class for the corresponding class): Cesar Moura
|
 |
Cesar Olavo
Greenhorn
Joined: Nov 19, 2004
Posts: 17
|
|
Ooops, Please reconsider the posting above. It's not exactly an answer for what you asked. The example I gave is a way of retrieving class objects, but *not* the one you asked. In fact Object.getClass() and Class.forName() have slightly different usages: Class c = myObj.getClass(); (class name is unknown, but there is an instance available) Class c = Class.forName(strg); (class name is unknown at compile time, but available at runtime) Cesar
|
 |
Glenny Dsilva
Ranch Hand
Joined: May 09, 2005
Posts: 42
|
|
That means at runtime we are loading a class based on a particular string. and then creating an instance,
|
 |
Neeraj Dheer
Ranch Hand
Joined: Mar 30, 2005
Posts: 225
|
|
yes, that is right. and the 'particular string' is the fully qualified class name.
|
 |
Glenny Dsilva
Ranch Hand
Joined: May 09, 2005
Posts: 42
|
|
|
Thanks a lot !
|
 |
Glenny Dsilva
Ranch Hand
Joined: May 09, 2005
Posts: 42
|
|
so in Class.forName we load the driver. Then why not directly create the instance of the Driver class when u know the class name why load it dynamically. pls. clear this point
|
 |
Neeraj Dheer
Ranch Hand
Joined: Mar 30, 2005
Posts: 225
|
|
Remember, to create a new instance using Class.forName(), you have would have to do something like Class.forName("driver").newInstance() The reason for using Class.forName() here is not to create an instance of the driver. The reason for using Class.forName() here is to only load the driver and register it with the JDBC DriverManager. what i mean by that is: when you actually do something like DriverManager.getConnection(...), the DriverManager must know which driver to use, where the driver resides etc. The DriverManager would not know all this if you only create an object for the driver using 'new' [ May 10, 2005: Message edited by: Neeraj Dheer ]
|
 |
 |
|
|
subject: Class.forName
|
|
|