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
posted
0
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
posted
0
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 ]