Meaningless Drivel is fun!
The moose likes JDBC and Relational Databases and the fly likes Load the class thru class.forName() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Load the class thru class.forName() method" Watch "Load the class thru class.forName() method" New topic

Load the class thru class.forName() method

Suresh Gangapatnam

Joined: Apr 12, 2012
Posts: 1
What happens internally while loading the class thru class.forName("Driver")?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46345
Welcome to the Ranch

I presume by Driver you mean something like com.databasecompany.drivers.Driver. If you look up the static Class#forName(String) method, you find it returns the Class<?> object of that name. It is a little like a static counterpart to the instance method Object#getClass(). Now, to get a Class<?> object, you have to have the Class in JVM memory, so if the class of that name has not yet been loaded, it loads it into JVM memory. And whenever you load a class into memory, its static members are loaded too, and any static initialisers are run (you can tell that from the list of Exceptions shown in the link.
Most JDBC driver classes have a static initialiser which sets up the connection to the database management program. Running Class.forName(...) at the start of the program will call that static initialiser and connect you to the database.
Ravikanth thota

Joined: Apr 09, 2012
Posts: 24
actually question should be Class.forName() [C-capital]

here Class is the name of the class belongs to java.lang package

forName() is the static method which loads the class from secondary memory to primary memory

here Class.forName() loads the driver class from secondary memory to primary memory.
I agree. Here's the link:
subject: Load the class thru class.forName() method
It's not a secret anymore!