File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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: 13875
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
Thanks a lot.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How all non static member of outer class are available to the non static method in inner class
 
Similar Threads
non-static inner class
Inner Access specifiers
classes working not understood
accessibility modifiers for classes
can inner class call outer class method?