Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner class  RSS feed

 
Ravi Tripura
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
B b = new B();
public void showI(){

System.out.println(b.i);
}
class B {
private int i = 2;

}

}

Hi there,

Iam trying the above code with a successful compile and run with the following code:
A a = new A();
a.showI();
Iam just wondering how am I able to access private member of class B from A.
thanks in advance,
Ravi.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Simple answer: because by definition, the classes B and A have access to all of one another's private data; or more generally, outer classes have full access to the members of their inner classes, and vice versa.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!