What happens with Class.forName(str) ?


The terse answer will be the class name mentioned as parameter will be loaded. Especially if we see in the JDBC, the driver is loaded and initialized.

If its asked to me, I will say : The method attempts to locate,load and link the class.

The JLS specification states that

“Loading refers to the process of finding the binary form of a class or interface type with a particular name, perhaps by computing it on the fly, but more typically by retrieving a binary representation previously computed from source code by a compiler, and constructing, from that binary form, a Class object to represent the class or interface.”

According to the JLS, it must be transformed from it’s binary representation to something the Java virtual machine can use, this process is called linking. Finally, the class is initialized, which is the process that executes the static initializer and the initializers for static fields declared in the class.

Well its lots of stuff done before the class is loaded.

Now in terms of JDBC, Class.forName( new Driver(“sqldriver”));

The call to Driver class is made, which contains the static method:

static{

java.sql.DriverManager.registerDriver(new Driver());

} // with try catch block.

The above line will register the driver and add it in the list of Vector. The elements are referred when DriverManager.getConnection() is done

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s