• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

why to use Class.formName("")

 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I stand corrected.

Thanks Ulf for pointing that out as I would had always been in the impression that Class.forName uses reflection.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You can't have everything. Where would you put it?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic