• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What Class.forName() does?

 
Ranch Hand
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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�.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 672
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Bruce, but how is that going to help answer this question, "Can I still get an object of type represented by className?"
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic