File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner class" Watch "Inner class" New topic

Inner class

Ravi Tripura

Joined: Jun 15, 2004
Posts: 3
class A {
B b = new B();
public void showI(){

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();
Iam just wondering how am I able to access private member of class B from A.
thanks in advance,
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


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:
subject: Inner class
It's not a secret anymore!