Meaningless Drivel is fun!
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 Building Microservices this week in the Design 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: 24190


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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Inner class
It's not a secret anymore!