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

Class.forName().newInstance() related query

S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Hi I have a query ,

What is the difference between Class.forName("").newInstance() and Class.forName("") ?

regards,
S
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Have you looked at the JavaDoc? What aspect of the documentation has you confused?

Are you confused about the difference between classes and instances?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
I am not able to understand the difference , I mean to say where should I use only Class.forName("") & where should I go for
Class.forName("").getNewInstance().
Suppose I am loading the driver in JDBC , so which procedure is better ?

Please give some idea ,

regards,
S
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

What does Class.forName() return?
What does Class.forName().newInstance() return?

Which one you need depends on what you need in the end; the thing returned by Class.forName() or the thing returned by Class.forName().newInstance().

As for which one you need for JDBC - unless the JDBC driver is badly written the first one should be enough.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
S Majumder
Ranch Hand

Joined: Jun 03, 2009
Posts: 244
Rob Prime wrote:What does Class.forName() return?
What does Class.forName().newInstance() return?

Which one you need depends on what you need in the end; the thing returned by Class.forName() or the thing returned by Class.forName().newInstance().

As for which one you need for JDBC - unless the JDBC driver is badly written the first one should be enough.


Thanks for your reply.

Can you explain , what actually Class.forName().newInstance() return and what Class.forName() return ?

regards,
S
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

That was what I was asking you, so you'd find the answer yourself. Check java.lang.Class if you don't know.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I believe the question is related to an old pre-Java 1.2 problem when loading Database Drivers.
When the JDBC Driver Class is loaded, it should register itself with the DriverManager, no surprise there.
While calling Class.forName() should load the Class, pre-Java 1.2 it did not load the Class until an instance of the Class was created, hence all of the JDBC examples from that era, and copied from there without comprehension, includes the Class.forName().newInstance() rather than just Class.forName()
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Ah, so it was not badly written JDBC drivers that caused those problems back then.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

As far as I know, in the latest version of the JDBC API it's not necessary at all anymore to call Class.forName("...") for JDBC drivers - a new mechanism was added to JDBC so that it's able to find JDBC drivers that are on the classpath by itself, without the need for the JDBC driver class to be loaded explicitly.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

What Jesper says, too.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

But that will only work if the JDBC driver uses the ServiceLoader mechanism; if it doesn't (and there are plenty of old drivers around that don't) Class.forName is still the way to go.
 
wood burning stoves
 
subject: Class.forName().newInstance() related query