wood burning stoves*
The moose likes Java in General and the fly likes inner class instantiation from outside the the outer class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "inner class instantiation from outside the the outer class" Watch "inner class instantiation from outside the the outer class" New topic
Author

inner class instantiation from outside the the outer class

Randy Gibbons
Greenhorn

Joined: Feb 06, 2005
Posts: 14
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

Joined: Oct 24, 2004
Posts: 116
You are not outside the outer class, so it compiles.

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



Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Hi Randy,

welcome at the Ranch!

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
Randy Gibbons
Greenhorn

Joined: Feb 06, 2005
Posts: 14
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

Joined: Oct 24, 2004
Posts: 116
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

Joined: Feb 06, 2005
Posts: 14
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

Joined: Oct 24, 2004
Posts: 116
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
 
wood burning stoves
 
subject: inner class instantiation from outside the the outer class
 
Similar Threads
What's the result of this inner class code?
Inner class
Regular Inner class
Doubt in S&B book - Inner classes
(Inner Classes) MyOuter.MyInner vs MyInner