This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Access Levels in Inner Classes

 
TC Rangarajan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:


I thought this program should get compiler error,as i am trying to access private constructor of Class B from Class C.But,it runs fine and gives output as 3.Can someone please explain me this?

This is already pre-existing question in this forum.See this Link
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it has something to do with the private holding for the top level class A, not for the inner classes themselves.
 
Laura Montes
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,

I think class C can access the private constructor of B because inner classes have a special relationship with the outer class (class A) and among their members (class B and class C). Class A and class B are like members of the outer class. So members have access among them even if they are private, that's why class C can access class B.

Hope it helps you a little
 
Sindhu
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i have explained the code to the best of my understanding. Hope its correct.please add on your opinion if there are any.

class A
{
public static void main(String[] args)
{
C someC = new A().new C();
// inner class 'C' can only be instantiated with an instance of its enclosing class 'A' from a static context.

System.out.println("output :" + someC.one.var);
}
class B
{
private int var=3;
private B() { }
}
class C
{
B one =new B();
/*It is possible to create an instance of inner class 'B' without explicitly providing the name of the enclosing class 'A' [for eg: like A.B one=new A().new B()]from a non-static context inside the enclosing class. This is possible because there is already an instance of the outer class.*/
/*Now we know that members of class 'B' is visible in class 'C'.Now accessing the member variables/functions of class 'B' will depend on the access control of those variables/functions.Private
members can only be accessed by members of the same class.Hence 'line Z' in class 'D' will give compile time error.*/

/*But again,access protection never prevents a class from using any member of another class, as long as one encloses the other, or they are enclosed by a third class.This is why the code complies fine.*/
}

}

class D

{

//A.B two=new A().new B(); (line Z)

}
 
Sindhu
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Access protection never prevents a class from using any member of another class, as long as one encloses the other, or they are enclosed by a third class."
- is mentioned in this link:
http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc6.html
 
Sindhu
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i now realise that is manual for java 1.1.1. Hope it holds true for later versions as well.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic