This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class

 
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
Marshal
Pie
Posts: 24208
35
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