File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "RTTI - Class.ForName( ) doesn Watch "RTTI - Class.ForName( ) doesn New topic

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:


"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 ??

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: 661

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:
subject: RTTI - Class.ForName( ) doesn't load my class
It's not a secret anymore!