This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
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: 24193


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!