This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Confusion in Class.forName()  :confused: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion in Class.forName()  :confused: " Watch "Confusion in Class.forName()  :confused: " New topic
Author

Confusion in Class.forName() :confused:

Glenny Dsilva
Ranch Hand

Joined: May 09, 2005
Posts: 42
I have read that Class.forName() is to load the class dynamically.

And one of the places it is used is to load the Driver in jdbc.

for e.g.

Class.forName("com.mysql.jdbc.Driver");

But instead of the above code can i just use

package com.mysql.jdbc.Driver;

Why do i need to use Class.forName() (i.e. load it dynamically);

Pls. explain
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
--> I have read that Class.forName() is to load the class dynamically.
--> And one of the places it is used is to load the Driver in jdbc.
--> for e.g.
--> Class.forName("com.mysql.jdbc.Driver");

Normally what you will do is have the driver in a properties file and then call Class.forName(myDriverProperty); That way by changing the value in the properties file you will be loading a different jdbc driver

--> But instead of the above code can i just use
--> package com.mysql.jdbc.Driver;

I think you mean import com.mysql.jdbc.Driver ??

so what do you do if you now want to use a postgres database driver.
You have to change and recompile your code to now import org.postgresql.Driver.

--> Why do i need to use Class.forName() (i.e. load it dynamically);
That's exactly why you need Class.forName(). To load the class dynamically. IE: No recompile necessary if you change databases.

cheers
Darryl
KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125
Hi guy,

import doesn't load the class until your first reference that class.


Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion in Class.forName() :confused:
 
Similar Threads
Class.forName()
Class.forName()
Multiple loading of a class thru Class.forName() ??
Class.newInstance
Getting exception while loading