Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName()

 
Praveen palukuri
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
any one can explain the exact need of Class.forName().
I know that i'll load jdbc driver if it is not loaded earlier. Other than this, is there any thing to say.

Thank you.
 
pradeep jaladi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


You can get detail description of class.forName(...) in Sun offical Documentation .

class.forName(....) is used to Load the specified class in to JVM.
Java Sun Documentation
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Praveen palukuri:
hi,
any one can explain the exact need of Class.forName().
I know that i'll load jdbc driver if it is not loaded earlier. Other than this, is there any thing to say.

Thank you.


That's pretty much all there is to it.

When a JDBC driver is loaded by Class.forName(), a static initialization block will cause it to register itself with DriverManager. After that, even if the class is explicitly unloaded, the driver is still available via the DriveManager.getConnection() method (which would force reloading of the class).

Class.forName() is used rather than the normal Java compilation to allow for programs to be compiled and built that don't know at compile-time which database they will be used with, and therefore don't know which JDBC driver is required.
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dynamic class loading is nice. Whether you use it for JDBC or something else it really can make your application flexibly to load concrete implementations of known interfaces at run time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic