| Author |
What Class.forName() does?
|
Bruce Jin
Ranch Hand
Joined: Sep 20, 2001
Posts: 666
|
|
What exactly happens when I load a class using Class.forName()? For example: Class.forName("aJDBCDriver"); Do I get an object? Why do it this way? Why not just use aJDBCDriver aDriver = new aJDBCDriver(); Thanks Bruce
|
BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design
Crazy Bikes created by m-Power
|
 |
David Weitzman
Ranch Hand
Joined: Jul 27, 2001
Posts: 1365
|
|
|
It seems you are touching two different issues here. The reason JDBC drivers are usually loaded into the virtual machine with Class.forName() is that it is a more flexible design choice. You can read the names of drivers from a configuration file at runtime, rather than writing them all into the program at compile time. The reason you must load them is that they have to be registered before they can recognize database URLs. They are supposed to register themselves when loaded into the virtual machine. As to what Class.forName() does, consult the Java API docs. It does some tricky stuff with classloaders and resources and who knows what else. The most straitforward definition is that it finds a class, loaded previously or somewhere it can be loaded from, and gets a java.lang.Class object representing it.
|
 |
greg philpott
Ranch Hand
Joined: Nov 10, 2000
Posts: 73
|
|
Bruce, When you use Class c = Class.forName("aJDBCDriver") the JVM attempts to load, link and initialize the class: aJDBCDriver using the current classloader. You do get an instance of the Class class this way (this can be confusing). If you want an instance of your class: aJDBCDriver, you need to do this: aJDBCDriver d = (aJDBCDriver)c.newInstance(); The above is an example of java Reflection (or introspection) The reason that it is sometimes done in this long-winded and complicated way is that sometimes the name of a class is not known(or does not exist) when the program is written. This could happen, for example (also see davids excellent example above)when interpreting or displaying some other code. Use Reflection only when you have to. [This message has been edited by greg philpott (edited September 26, 2001).]
|
 |
Bruce Jin
Ranch Hand
Joined: Sep 20, 2001
Posts: 666
|
|
This is indeed confusing and interesting. If I get a class name dynamically, for example String className = getClassName(); Then I load this class Class c = Class.forName(className); Now I only have an object c which is of type Class. Can I still get an object of type represented by className? Bruce
|
 |
greg philpott
Ranch Hand
Joined: Nov 10, 2000
Posts: 73
|
|
yes you can. once you have your Class instance you can call the method newInstance() on it to get an Object that will be of type className. newInstance returns an Object though, so You will have to cast it to className type.
|
 |
Bruce Jin
Ranch Hand
Joined: Sep 20, 2001
Posts: 666
|
|
But I don�t know the class type name how can I cast? Can I do this? className d = (className)c.newInstance(); Note here className is a variable and it may have a value of className=�MyClass�.
|
 |
Michael Ernest
High Plains Drifter
Sheriff
Joined: Oct 25, 2000
Posts: 7292
|
|
Hi Bruce - Let me rephrase the problem; maybe that will make the answer easier to accept. Let's say we have a String that names a package-qualified class. We use that String in a Class.forName() call to get a Class object. We then want to use that same String to cast the return of clss.newInstance() back to the correct type. From a surface view, this seems pretty reasonable: But now, you've got to tell the compiler what type you're trying to retrieve. And if you already know what type you're trying to get, there's no value in code to leaving the casting type open to interpretation. Even if you could cast dynamically (which you can't), you certainly can't declare a type assignment dynamically; this defeats the idea of enforcing type-safety at compile-time. If we could in fact replace ??? with a run-time reference, we'd be saying that correct type is only enforceable at run-time. There are ways to get at what you want, but sooner or later someone has to check type at compile-time. ------------------ Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide [This message has been edited by Michael Ernest (edited September 26, 2001).]
|
Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
|
 |
pjoisha
Ranch Hand
Joined: Mar 06, 2001
Posts: 123
|
|
HI all, I have tried asking this question unsuccessfully before and now that this class creation topic is being discussed I thought I should try again Why is the piece of code in (1) more "advantageous" than approach (2) Smile aSmile= (Smile)getClass().getClassLoader().loadClass("Smile").newInstance(); // (1) Smile aSmile = Class.forName("Smile").newInstance(); // (2) TIA, PJ
|
 |
Bruce Jin
Ranch Hand
Joined: Sep 20, 2001
Posts: 666
|
|
Hi Michael and all: I am thinking I may be able to cast the unknown type to an Object type. Text books say Object object can hold anything. Can we replace ??? by Object? Like this String str = "mfe.thing.Smidgen" Class clss = Class.forName(str); Object smidge = (str)clss.newInstance
|
 |
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
|
|
|
Yes, Bruce, but how is that going to help answer this question, "Can I still get an object of type represented by className?"
|
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
|
 |
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
|
|
Originally posted by Prashanth Joisha: Why is the piece of code in (1) more "advantageous" than approach (2) Smile aSmile= (Smile)getClass().getClassLoader().loadClass("Smile").newInstance(); // (1) Smile aSmile = Class.forName("Smile").newInstance(); // (2)
Seems to me that approach (2) would be more advantageous, Prashanth.
|
 |
 |
|
|
subject: What Class.forName() does?
|
|
|