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?
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.
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
Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
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.
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 ]
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.