This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner class" Watch "Inner class" New topic
Author

Inner class

Ravi Tripura
Greenhorn

Joined: Jun 15, 2004
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class
 
Similar Threads
Private member access
polymorphism n overridng
constructors
Query related to com.bea.xml.XmlObject
Default constructor