tags:

views:

101

answers:

3

Why I need to load the class definition like :

Class.forName("ClassName");

What is the need and advantage of this .Typically which is used to load driver class in JDBC.

+1  A: 

You don't, really. ClassName.class will work just as well. Anyway, getting the class definition in this manner is generally the way that SPI implementations are injected into APIs.

Chris Dennett
+5  A: 

What is the need and advantage of this. Typically which is used to load driver class in JDBC.

It allows you to build your applications so that key external dependencies are not compiled into the application source-code.

For example, in the JDBC case, it allows you to switch between different driver implementations, and (in theory) different database vendors without changing your source code.

Another use-case is when some supplier develops a generic form of an application with extension points that allow customers to "plug in" their own custom classes. The custom classes are typically loaded using Class.forName(...).

A third use-case is application frameworks and containers which typically use Class.forName(...) under the hood to dynamically load the classes for application-specific beans, servlets, and so on.

A fourth use-case is where the application (or more likely an application library) has modules that are not used in a typical application run. By using Class.forName(...) internally, the application or library can avoid the CPU and memory overhead of loading and initializing large numbers of unwanted classes. (The Sun Swing libraries apparently do this to reduce application startup times, and I'm sure there are other examples.)

However, if you don't need to be able to do this kind of thing, static dependencies are simpler to implement.

FOLLOWUP

But here ,while compile itself the "ClassName" parameter is known .So the key external dependency is compiled into application source-code??

Nope. Obviously, that defeats the purpose. The application (or the framework) typically determines the names of the classes to be dynamically loaded from some configuration file.

Stephen C
But here ,while compile itself the "ClassName" parameter is known .So the key external dependency is compiled into application source-code??
JavaUser
I was unable to verify your 4th use case. The Java Language Spec, in section 12.4.1, defines that the virtual machine must not initialize a class until an instance of that class is created, a static member used, or an assert statement lexically within the class executed. Merely referring to a class does not initialize it. The time of loading is at the discretion of the virtual machine, though according to -verbose:class, both Sun's client and server vm for windows will defer loading until initialization is required.
meriton
A: 

Simplest reason why Class.forName(string className) is used are

  • In JDBC the statement is used to load and register the DriverClass with the DriverManager. The other method to do this is using registerDriver(Driver obj) method which takes in an object of the driver class.Using the above statement helps us avoid handling the driver object directly.
  • Most of the applications use property files for defining JDBC connection and driver properties.This type of dynamic loading helps us to make the application more portable as the driver can be configured without changing the source.
frictionlesspulley