Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes RTTI - Class.ForName( ) doesn't load my class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "RTTI - Class.ForName( ) doesn Watch "RTTI - Class.ForName( ) doesn New topic
Author

RTTI - Class.ForName( ) doesn't load my class

Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
I am doing some examples from Bruce Eckel's Thinking of Java. But some of the code examples aren't behaving as expected. In the following example:



The output of the code is:



But Bruce reckons is should be:



because:

"Class.forName() returns a class reference, which is being ignored here. The call to forName() is being made for its side effect, which is to load the class Gum if it isn't already loaded. In the process of loading, Gum's static clause is executed."

So why doesn't the code behave as expected? I've copied it out exactly as the example in the book. I'm a little lost on this one.

Thoughts appreciated.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Maybe the gum got stuck


I have seen it pointed out once earlier as well that some code in Bruce's book wouldn't compile/run as expected. Maybe that's the case with this snippet as well. You wanna try sending this to Bruce himself at www.bruceeckel.com ??


ASCII silly question, Get a silly ANSI.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Read the API for Class.forName. Here's a relevant quote:

Given the fully qualified name for a class or interface (in the same
format returned by getName) this method attempts to locate, load, and link the
class or interface.


The emphasis is mine. Ahem.

And I hope you've already put a printStackTrace in that catch block to see
what went wrong instead of just sitting and waiting for BRUCE ALMIGHTLY
to come to the rescue...
[ November 17, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

I suppose, the problem should be in your package declaration.


If you are not laughing at yourself, then you just didn't get the joke.
Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
Thank you for the replies.

If I change the code to:


Everything works as bruce suggests.

Just for reference: A fully qualified name is the classname including the package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RTTI - Class.ForName( ) doesn't load my class
 
Similar Threads
what's this static mean
Question Regarding class loading precedence.
Difference between Class.forName("ClassName") and ClassName.class
Doubt regarding Class.forName() in JDBC connection
Overloading wih static methods