Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reg class.forName()

 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

to load the driver class we use class.forName("class") what it will do?
can't we create driver class object using "new" key word?? waht is the difference?? Please explain me in detail.......

Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a read through our JDBC FAQs. This question get asked a lot.
 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
Have a read through our JDBC FAQs. This question get asked a lot.


Hi,
I am no where find this question in above link..please provide me the answer
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swapna,
"forName()" also registers the driver with the "DriverManager" class, which "new" does not do.
So if you use "new", you also have to ensure, in your code, that you explicitly register the driver with "DriverManager".

Good Luck,
Avi.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"forName()" also registers the driver with the "DriverManager" class, which "new" does not do.
So if you use "new", you also have to ensure, in your code, that you explicitly register the driver with "DriverManager".

This is not correct. The driver registers itself with the DriverManager when the class gets loaded (that's done in a static initialization block). Since the class must be loaded in order to instantiate it, the registration will happen without anything needing to be done by the user code. See the javadocs for java.sql.Driver for more information.
 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply

but i didn't get the answer:-(,please provide me the difference and why we will use class.for.Name() not in JDBC generally......
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the docs .
 
ashwini allamsetti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Its simple basically..
check the syntax out : Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
is not it?
So over there forName() is a static method available in the class called Class and the String argument shows the path where the drivers are available..
and the answer for your question is here..
since forName() is a static method otherwise a class method..what all we don't need is to create an object...object creation essentially requires heap area to be allocated and this case object creation is meaningless for a static method.

Comments for my answer are most welcome..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic