Forums Register Login

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

+Pie Number of slices to send: Send
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??


+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
means if i m using

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

i should not worry about newInstance().
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
okay..thanks...
+Pie Number of slices to send: Send
You're welcome.
+Pie Number of slices to send: Send
 

Rob Spoor wrote:You're welcome.

Same here
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2646 times.
Similar Threads
jdbc
difference between new and Class.forName().newInstance()
Connection to MySQL
org.gjt.mm.mysql.Driver
Using jdbc from jsp with MySQL
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:32:18.