Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forName().newInstance() related query

 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have a query ,

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

regards,
S
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the JavaDoc? What aspect of the documentation has you confused?

Are you confused about the difference between classes and instances?
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, so it was not badly written JDBC drivers that caused those problems back then.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Jesper says, too.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic