• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner Classes

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought we cannot access private variables of the inner class in the outer class. I surprised when code is compiled when private variable of the inner class is accessed in outer class.

Please find the code:

package Test.InnerClasses;

class Inner4
{
int iyear = 1973;
public static void main(String... arg)
{
Inner4 inner4 = new Inner4();
inner4.ewold();

}
public void ewold()
{
LimeTree tree = new LimeTree();
tree.nyork();
}
private class LimeTree
{
private void nyork()
{
System.out.println(" Iyear is" + iyear);
}
}
}


my question is:

if you are defining any private members , then those members can be accessed in the class only , then how can we access private members of the inner class in the outer class?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my question is:

if you are defining any private members , then those members can be accessed in the class only , then how can we access private members of the inner class in the outer class



I hope you know that Inner classes (regular inner classes --not method local) can access private members of enclosing class , as like other class members --- did you give it a thought why so..?

Reason is -- Inner class is treated as a member of outer class --similar to methods and variables of a class.
So Inner class can access private members of enclosing class and enclosing class can access private members of inner class..

I hope this clears the confusion
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi prajal,

thanks for your reply.

but class LimeTree

has private method
private void nyork()
{
System.out.println(" Iyear is" + iyear);
}


which is accessed by Inner4 class at

LimeTree tree = new LimeTree();
tree.nyork();

LimeTree is a inner class for Inner4. So LimeTree can access all the variables of Inner4. But how can Inner4 can access the private member(s0 of Inner4? i mean Inner4 is not member of LimeTree .

this is my confusion.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Remember Innerclass can access private members of outer class and Outer class can access private members of innerclass provided in both cases the private members are being accessed within the class.
Inner classes are treated as members of outer class which means the private[or any members] members of inner class are treated by its enclosing class as if they were members of its own class. So as you know in a normal case any private member can be accessed only within the class , The same concept can be applied to inner and outer class memebers.

Innerclass can access private members of outer class via an implicit reference of outer class. This implict reference of outer class can be accessed in your inner class through "MyOuter.this".

Outer class can access private members of innerclass via an instance of the inner class.
Check out the below code:


I hope this clears your confusion.
Suggestion : Use better names for your class , for example your inner class is called LimeTree and your outer class is called Inner4. It can cause confutions when your trying to learn the concept.
Thanks
Deepak
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More generally, the private members of an inner class can be accessed anywhere else within that top-level class--including by another inner class that has no inheritance or container relationship with the first inner class.

I posted some sample code illustrating this in another thread from last week. Hope this helps!
 
reply
    Bookmark Topic Watch Topic
  • New Topic