wood burning stoves 2.0*
The moose likes Java in General and the fly likes How all non static member of outer class are available to the non static method in inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How all non static member of outer class are available to the non static method in inner class" Watch "How all non static member of outer class are available to the non static method in inner class" New topic
Author

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

siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
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

Joined: Aug 16, 2005
Posts: 14193
    
  20

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
Thanks a lot.
 
GeeCON Prague 2014
 
subject: How all non static member of outer class are available to the non static method in inner class