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
earlier." [When you pass just the class name, the initialize parameter defaults to true.]

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
"Second, a programmer can explicitly load! a driver class using the standard Class.forName method. For example, to load the acme.db.Driver class you might do:=20
Class.forName(acme.db.Driver);
In both cases it is the responsibility of each newly loaded Driver class to register itself with the DriverManager, using the
DriverManager.registerDriver method. This will allow the Driver-Manager to use the driver when it is attempting to make database connections."

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! .
The Driver class is required to register itself when it is loaded. The Driver class can only register itself if it has a reference to an instance of itself. So one of the few ways (perhaps the only way?) that a newly loaded class can register itself is to have a static initialization block which creates an instance and calls=20

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
this:

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:
1) Loading
2) Linking
3) Initialization

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
reoslution of the class may occur at anytime later.

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:
> 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");

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:
lets say Mr.X uses:
Driver d = new oracle.jdbc.driver.OracleDriver();

and Mr.Y uses: Class.forName(ClassConstants.DriverName).newInstance();

public final class ClassConstants{
     public final static String DriverName="oracle.jdbc.driver.OracleDriver"
}

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
for change?

Vinay

Related:

Java Books
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.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.