This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes (Inner Classes) MyOuter.MyInner vs MyInner 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "(Inner Classes) MyOuter.MyInner vs MyInner" Watch "(Inner Classes) MyOuter.MyInner vs MyInner" New topic
Author

(Inner Classes) MyOuter.MyInner vs MyInner

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Hi!

I think there is no difference between in the following:

MyOuter.MyInner i1 = new MyInner();
and
MyInner i2 = new MyInner();

I just want to confirm, because in the book I'm studying, the authors only teach the first way.

Thanks.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Either of those two ways is fine when you want to instantiate a static inner class from within its enclosing class. If you are going to instantiate a static inner class from outside the enclosing class, then you need to use the first form.


All code in my posts, unless a source is explicitly mentioned, is my own.
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
That's fine if your instantiating the class from within the enclosing class. Otherwise, you'd use:



or




And, if the class is static



Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Thanks!

Ryan Beckett wrote:That's fine if your instantiating the class from within the enclosing class. Otherwise, you'd use:

or



It is possible to instantiate a regular inner class this way from other classes.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: (Inner Classes) MyOuter.MyInner vs MyInner
 
Similar Threads
Instantiating inner class
object of inner class - where can't we make
inner class reference
Confusion over inner classes
I get a runtime error when using static Inner Class -- pls explain.