• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Load the class thru class.forName() method

 
Suresh Gangapatnam
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens internally while loading the class thru class.forName("Driver")?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic