This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class.forName  RSS feed

 
Glenny Dsilva
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Cesar Olavo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means at runtime we are loading a class based on a particular string.

and then creating an instance,
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, that is right.

and the 'particular string' is the fully qualified class name.
 
Glenny Dsilva
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot !
 
Glenny Dsilva
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!