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

How all non static member of outer class are available to the non static method in inner class

 
siddharth das
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you please tell the logic behind this.

How all non static member of outer class are available to the non static method in inner class?

Thanks,
Siddharth
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a non-static nested class, each instance of that nested class is associated with an instance of the enclosing class. Have a look at this:

Note how I create the new instance of Example in line 14: Example example = main.new Example();. So I'm creating a new Example object here that is associated with a specific instance of class Main. The instance of Example can access the member variables, such as id, of that instance of Main.

That's just how non-static nested classes work in Java.
 
siddharth das
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic