Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class instantiation from outside the the outer class

 
Randy Gibbons
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Explanation?

[I added [ code ] tags - Jim]
[ February 07, 2005: Message edited by: Jim Yingst ]
 
Robert Hayes
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not outside the outer class, so it compiles.

Try this (same package), it will not compile:



 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randy,

welcome at the Ranch!

For better readability, please use code tags when posting source code. Thanks, and have fun!
 
Randy Gibbons
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Robert Hayes
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Randy Gibbons
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robert Hayes
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Randy,

Yes I agree, both books are really good. If you're studying for the cert (?) you'll probably want to do some mock exams. That stuff is over on the SCJP forum
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic