aspose file tools*
The moose likes Java in General and the fly likes why to use Class.formName( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why to use Class.formName("")" Watch "why to use Class.formName("")" New topic
Author

why to use Class.formName("")

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi All

When we get a connection for a DB, we generally load a class using Class.forName(). I would like to know if there is a specific reason why we only use this way and not directly create an instance. The only reason that comes to my mind is that because we only need the class to be loaded and not really need an instance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
One reason is that we'd like to avoid hard-coding DB access parameters, even the name of the driver class. It is not uncommon to switch drivers; thus its name should be kept out of the source code, e.g. in some config file.

And, yes, the JDBC code does not need an instance, it only nneds to make sure that the class is loaded (which will cause it to register itself with the DriverManager).


Ping & DNS - my free Android networking tools app
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Thanks for the reply.

Yup that sounds like a really good reason to use reflection for class loading if the class name is fetched using a config file.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
Class.forName does not use reflection (it was part of the API before the reflection API existed). It's just that classes can be loaded by their fully qualified class name.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I stand corrected.

Thanks Ulf for pointing that out as I would had always been in the impression that Class.forName uses reflection.
Arnav Velimala
Ranch Hand

Joined: Jun 04, 2007
Posts: 37
Hi Anupam,

The only reason that comes to my mind is that because we only need the class to be loaded and not really need an instance.


Are you sure that it is not really required to instantiate the class.

I believe in the Driver classes there will be a static block that will instantiate the class.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
Originally posted by Arnav Velimala:
Are you sure that it is not really required to instantiate the class.

I believe in the Driver classes there will be a static block that will instantiate the class.


It helps to be precise here. What Anupam refers to as "we" is the client program, and that does not need an instance of the driver - it needs objects that implement the relevant interfaces. Of course there's an instance of the driver class being involved somewhere, but not (directly) in the client code.

You're right that loading the class will lead to the driver creating an instance of itself, as per the java.sql.Driver javadocs.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why to use Class.formName("")