Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(Inner Classes) MyOuter.MyInner vs MyInner

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic