aspose file tools*
The moose likes Java in General and the fly likes Difference between Class.forName() and Class.forName().getInstance() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between Class.forName() and Class.forName().getInstance()" Watch "Difference between Class.forName() and Class.forName().getInstance()" New topic
Author

Difference between Class.forName() and Class.forName().getInstance()

Kalyan Patti
Greenhorn

Joined: Jun 27, 2006
Posts: 22
Hi,

I am really very much confused Difference between Class.forName() and Class.forName().getInstance()
Can anyone explain me the difference between the two.

Thank you,
Sravan.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
What class does getInstance() belong to?
I don't see it in the Class class.
Kalyan Patti
Greenhorn

Joined: Jun 27, 2006
Posts: 22
Yes, there is no method getInstance() for Class. But people usually use it to load the class dynamically.

Sravan.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
What class is getInstance() in?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

He means "newInstance()".

Class.forName() returns a Class object -- an object that represents a particular Java class. newInstance() -- a method of java.lang.Class -- will use the no-argument constructor of the represented class to create an instance of that class.


[Jess in Action][AskingGoodQuestions]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

One example I can think of is the old days of JDBC. You can still see examples of this workaround in current code through the wonders of "cut and paste".

As EFH says, Class.forName() gets a reference to a Class, Class.forName().newInstance() tries to use the no-arg constructor for the Class to return a new instance. No surprises so far. Another common use for Class.forName() is to cause the Class to be loaded, since some types of Classes have side-effects from the loading process which is required for other purposes. JDBC is a large user of this process, since the Driver class is required to register itself with the DriverManager Class when it gets loaded.

In the deep dark days of Java, probably v1.1.8 but possibly up to Java 1.2, there was an issue that the default ClassLoader would not load a Class until it had an instance created. In these cases, JDBC code would fail if you used Class.forName() rather than Class.forName().newInstance().

While newInstance() would create an instance that got immediately thrown away, it was required to make Class.forName() work correctly. This work around is no longer required, but I am amused to still see it in code being written to this day.

There was a similar ClassLoader problem which was solved by attaching a non-running static thread to a Class to prevent it being unloaded by the ClassLoader, but again I haven't seen any need for this for many a year.

/Dave
Kalyan Patti
Greenhorn

Joined: Jun 27, 2006
Posts: 22
Thanks David O'Meara and Ernest Friedman-Hill. You have given lot of information and cleared my doubt conceptually. I specially thank David O'Meara for his detailed explanation.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

We try. I'm glad we managed to help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between Class.forName() and Class.forName().getInstance()