• 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

inner class instantiation from outside the the outer class

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not outside the outer class, so it compiles.

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



 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
This tiny ad is wafer thin:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic