Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Access Levels in Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Access Levels in Inner Classes" Watch "Private Access Levels in Inner Classes" New topic
Author

Private Access Levels in Inner Classes

TC Rangarajan
Greenhorn

Joined: Apr 15, 2008
Posts: 3
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

I think it has something to do with the private holding for the top level class A, not for the inner classes themselves.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Laura Montes
Greenhorn

Joined: Aug 03, 2007
Posts: 9


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


SCJP 5<br />"We are, each of us, ANGELS with only one wing & we can only fly by embracing one another"
Sindhu
Greenhorn

Joined: Apr 14, 2008
Posts: 3
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

Joined: Apr 14, 2008
Posts: 3
"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

Joined: Apr 14, 2008
Posts: 3
As i now realise that is manual for java 1.1.1. Hope it holds true for later versions as well.
 
GeeCON Prague 2014
 
subject: Private Access Levels in Inner Classes