Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Re:Class.forName()

 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have a query.The function forName(String className) of java.lang.Class returns class object associated with the given string.Every return type must be caught.But if I write a programm such as


public class Demo{
public static void main(String arg[]){
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//this function returns class object
/* code to manipulate database */


}
}

Eventhough I have not caught the returned class object as Class m = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"), the program working fine.I just want to know how is this possible.Please clarify my doubt.

Regards,
Sujith
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of small points: Classes don't need to be caught. Only checked Exceptions need caught. Also Java has methods not functions.

It is perfectly legal Java to call a method which returns a value and do nothing with the value. For example:

Will compile no problem, despite the fact that valueOf returns a Boolean.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A 'side effect' of this method is that it causes the Class to be loaded, but only if it has not already been loaded. The trick here is that a Driver has a 'static block' which is a section of code that gets run when the class loads.

Therefore the forName() loads the Class, the static block fires, and the Driver registers itself with the DriverManager. This only gets done once, since the Class won't be loaded again.
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

but what happens to the returned value of the above method call? where is it going to be stored ?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not stored anywhere. Since there is no assignment, the compiler will discard the object.
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for clarifying my doubt.
 
Rashi Gulati
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SUJITH SUNIL KANAPARTHI

Class.forName(jdbc dbc:YourDriver) actually work this way:
YourDriver is a class which has a static block in that static block you are creating the instance of YourDriver class which in turn will call the DriverManager.registerDriver and hence will get registerd with the DriverManager as DriverManager maintains the list of all the drivers registered with it.
Something like this
class YourDriver implements Driver
{
static
{
YourDriver d = new YourDriver();
DriverManager.registerDriver(d);
}
}

Instead of writing Class.forName(jdbc dbc:YourDriver) you can also write a command on the command prompt like this
jdbc.drivers = jdbc dbc:YourDriver :jdbc dbc:YourDriver2:jdbc dbc:YourDriver3
and so on by this you are actually adding the Drivers to the java.lang.System property so when the DriverManager class is intialized it looks for jdbc.drivers if user has entered drivers then it will load them, by this you can specify more then one driver seprated with :

I hope this will help
Take care
Rashi
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of writing Class.forName(jdbc dbc:YourDriver) you can also write a command on the command prompt like this jdbc.drivers=jdbc:odbc:YourDriver:jdbc:odbc:YourDriver2:jdbc:odbc:YourDriver3

Not quite right.
You can put them as -D options, and they have to be the Driver class, not the JDBC URL eg

java -Djdbc.driver=oracle.jdbc.OracleDriver;com.next.jdbc.Driver com.your.class.to.Run
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SUJITH SUNIL KANAPARTHI,

Please do everyone a favor and adjust your display name to used mixed case rather than all uppercase, which is rather jarring.

thanks
bear
Forum bartender
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I hope so. I've been delaying on commenting.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic