• 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

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

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic