File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Difference between Class.forname( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Difference between Class.forname(" ") and Class.forName(" ").newInstance()" Watch "Difference between Class.forname(" ") and Class.forName(" ").newInstance()" New topic
Author

Difference between Class.forname(" ") and Class.forName(" ").newInstance()

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 928
is there any difference between



and



however in the second statement a instance of the Class is created but both are giving the same output, so i want to know is there any difference between both of them??


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

The first only loads the class, the second also creates an instance. For some (mostly old) drivers this was necessary because the class wouldn't register itself properly without it. You're using an old driver though. The latest one is com.mysql.jdbc.Driver, and doesn't need the newInstance() call.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
You do not need an instance of the class. If your class has a private constructor, it may throw a (checked) Exception. You should miss out the newInstance call because you do not need the instance.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 928
means if i m using

Class.forName("com.mysql.jdbc.Driver");

i should not worry about newInstance().
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Campbell Ritchie wrote:You do not need an instance of the class. If your class has a private constructor, it may throw a (checked) Exception. You should miss out the newInstance call because you do not need the instance.

If I recall correctly, a few quite old JDBC drivers actually did need an instance. They were poorly written to register the driver in the constructor, not a static block. These days that should definitely not be the case anymore.

Punit Jain wrote:means if i m using

Class.forName("com.mysql.jdbc.Driver");

i should not worry about newInstance().

Nope. Just drop it.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 928
okay..thanks...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

You're welcome.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
Rob Spoor wrote:You're welcome.
Same here
 
 
subject: Difference between Class.forname(" ") and Class.forName(" ").newInstance()
 
Similar Threads
Connection to MySQL
jdbc
org.gjt.mm.mysql.Driver
difference between new and Class.forName().newInstance()
Using jdbc from jsp with MySQL