This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName

 
shar ball
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is said that Class.forName(String driver), loads the driver and register the driver with the Driver Manager. But how does Class.forName() register the driver with the Driver Manager? It is a simple method from the "Class" class.
Can anyone let me know about this?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName(String) does not register the class with the DriverManger. The DriverManager looks for a loaded class which can support the JDBC url it has been asked to get a connection with. Class.forName(String) loads and initializes the class of that name, once that is done the DriverManager can find it.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
more specifically when clsss.forName() loads the driver before initialisation, driver is supposed to register itself with DriverManager using its static initializer.

As per JDBC guide by Sun


All Driver classes should be written with a static section (a static initializer) that creates an instance of the class and then registers it with the DriverManager class when it is loaded. Thus, a user would not normally call DriverManager.registerDriver directly; it should be called automatically by a Driver class when it is loaded



Shailesh
 
sinasi susam
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why dont we import all namespaces of Driver and DriverManager classes?

Why; Class.forName("...");

what does it do?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use a class directly, the JVM will load the class and initialize it automatically. For exaple, when you do something like

The String class is loaded and initialized. This means that memory for static variables is allocated and their values are initialized. Static initialzers are also executed. Similarly when you do

The Connection and DriverManager classes are loaded (if they aren't already loaded, that is) before the getConnection() method is even called.

However, we never use JDBC driver classes directly, so how are they loaded? By calling Class.forName()! This method will also load a class if it isn't already loaded. When the driver class is loaded, the static initializers in the class run. This is when the driver is registered with the DriverManager, as described above.

If you are interested in learning more, I suggest you look at the javadocs for Class, specifically the forName() method. You should also look at ClassLoader.

Layne
 
sinasi susam
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String class is loaded and initialized. This means that memory for static variables is allocated and their values are initialized. Static initialzers are also executed.


what do you mean by that?

That was not a static variable?Am i right?




I think if its something like this, its initialized.Because we dont need to get new insatance of class A.We will reach it by A.str.However, i dont know when they are initilized?
[ July 16, 2005: Message edited by: sinasi susam ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, a static initialiser is a special block of code that will only be executed once when the Class is loaded. It is possible to load a Class several times in a single JVM and even reload a Class, but for simplicity sake assume that the block will only be run once when the class is first loaded.

It looks like this:



I haven't compiled this, but it should work. Class.forName() causes the static block to 'fire' the first time, the static block creates an instance and registers it with the DriverManager. Subsequent calls to Class.forName() don't do anything.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic