Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Kathy Sierra and Bert Bates' "Sun Certified Programmer ... Study Guide" page 462 states that "From outside the outer class code (including static method code within the outer class), the inner class name must now include the outer class name," as in MyOuter.MyInner. I see the same statement in many other books. Yet from static method code within the outer class, I am able to compile and run fine without including the outer class name, as in the following example from p. 463 of the same book:
[I added [ code ] tags - Jim] [ February 07, 2005: Message edited by: Jim Yingst ]
For better readability, please use code tags when posting source code. Thanks, and have fun!
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Yes, but you'll notice Kathy and Bert define "outside" to include "static method code WITHIN the outer class" (my CAPS). Eckel, in Thinking in Java Third Edition, page 333, says the same thing: "If you want to make an object of the inner class anywhere except from within a non-static method of the outer class [in other words, including from within a static method of the outer class], you must specify the type of that object as OuterClassName.InnerClassName, as seen in main() [where the main() he is referring to is WITHIN the outer class of his exampe]. Similarly, I can omit the OuterClassName from Eckel's example, and it compiles and runs fine. Am I misunderstanding what I am reading?
I think you're getting hung up on the declaration itself. What they are trying to say is that to instantiate a non-static inner class, you always need a reference to an object of the outer class... (which you seem to understand)
What they should then go on to say is "you can't instantiate the inner class from a static method of the outer class WITHOUT EXPLICITLY REFERENCING THE OUTER CLASS OBJECT" (caps are my words, the rest are from page 461 of K&B).
Check out the code below... because a static method doesn't have an implied reference to its object, you can't just use "new MyInner();" You must use "new MyOuter().new MyInner();"
Your second point is regarding the reference declaration: Yes, within the Outer Class itself, you don't need to qualify the reference with "MyOuter." because you're already in it...
However, if you look at AnotherClass you MUST use "MyOuter.MyInner" because AnotherClass has no idea where MyInner is...
I agree it's a bit confusing. Hopefully I didn't confuse you more...
[ February 07, 2005: Message edited by: Robert Hayes ]
Thanks! Your code certainly shows all the possibilities. I believe both K&B and Eckel slightly misstate things, in that the OuterClassName in the expression OuterClassName.InnerClassName is NOT required when used in static code of the outer class, and that should probably be explicitly stated. But I bow before the awesomeness of both their books, and I rest content that, as a greenhorn, I got the need for the reference to the outer class and the ref.new syntax down.