File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC 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
Bookmark "Load the class thru class.forName() method" Watch "Load the class thru class.forName() method" New topic
Author

Load the class thru class.forName() method

Suresh Gangapatnam
Greenhorn

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

Joined: Oct 13, 2005
Posts: 38788
    
  23
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
Greenhorn

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: http://aspose.com/file-tools
 
subject: Load the class thru class.forName() method