Difference
between without newInstance() and with =newInstance()
What difference is between without newInstance() and with = newInstance() method ?? Use the spec: You really mean Class.forName, right? There is no method Class.forClass that I can find. There is an ObjectStreamClass.forClass method. When using Class.forName(), you (should) never need to call newInstance(). The Java 1.2 API says this about calling Class.forName(): "Given the fully qualified name for a class or interface
(in the same format returned by getName) this method attempts to locate,
load, and link the class or interface..... The class is initialized only
if the initialize parameter is true and if it has not been initialized
Checking the Java Language Spec, Chapter 12: "Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class." The JDBC 1.2 spec says
So, one of the two specified ways of loading a driver is to call Class.forName(), with no requirement to call newInstance(). (The other way is to list drivers using a system property.) Your particular driver may require you to call newInstance() but it is not required by the specification. In addition, if you need to call newInstance(), then why bother with Class.forName().newInstance() when you know the class name at compile time; you can get the same effect with new Driver("driver_name"); Back to the Spec! .
DriverManager.registerDriver. I have specific experience with the class oracle.jdbc.driver.OracleDriver.
In fact, I decompiled it so I could be sure of what it does. The OracleDriver
class registers itself as I described above: it has a static initialization
block in which it creates an instance of itself and then passes that reference
in the registerDriver call. The decompiler that I used renders the code
like
DriverManager.registerDriver(new OracleDriver()); I would wager that all the driver classes register themselves using a static initialization block.=20 So, the correct way according to the Java API for Class and the specification is to call Class.forName("Driver"); Without the need to call anything else. You shouldn't need to call registerDriver() as I've sometimes seen in examples. You shouldn't need to call newInstance().=20 Sowjanya Devi Uddaraju ------------------------------------------------------------------------------ The first three phases in the lifetime of a java type
are:
When you call class.forName(Drivername) the type represented
by the DriverName is loaded, linked(optionally resolved) and initialized.
Initialization according to the JVM spec should occur at the "first active
use" of the type. The optional
Invoking class.forname() is probably treated as "first active use" of the class but there are some inconsistencies in implementations. Some of the JVM implementations do not treat this as first active use and hence they do not initialize the class. When the Driver class is not initialized the driver does'nt get registered with the DriverManager since the code to get registered with the DriverManager is written in the static initializer block of the Driver class. These inconsistent implementations will have to initialize the class during instantiation(come what may :)) and hence if you are invoking the newInstance() method after loading the driver class using Class.forName()you can rest assured that your app would would work on any implementation. Yeah I agree it aint our headache about inconsistencies but remember its a wild world out there you neva know which JVM implementation ur APPs gonna run on so better take care of some known issues like these. All it takes is an extra instantiation. A small comment:
By doing so(by using the new operator instead of newInstance()) you are'nt using Java's Dynamic extension capabilities. You are coupling the code strongly with the type name. Its open for change. If you have done this in quite a few places its a mess. for ex:
and Mr.Y uses: Class.forName(ClassConstants.DriverName).newInstance(); public final class ClassConstants{
If both MR.X and Mr.Y have put in this code in many places
and their respective companies are changing the database vendor Mr.X will
have to search for all those places where he has used the new operator
for instantiating the Driver class whereas Mr.Y has to change it in one
place that is ClassConstants. Does'nt this make Mr.Y's class open for extension
but closed
Vinay
Related:
Java Certification, Programming, JavaBean and Object Oriented Reference Books Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|