Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose, the problem should be in your package declaration.
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic