Constructor visibility of inner classes
Hi,
I just ran into a problem that kind of baffled me:
privateclass Aimplements AAA
{
....
privateclass Bimplements BBB
{
private B(){}
public B(int mode)
{
...
}
}
}
A is an inner class of some class and B in turn is an inner class of A. Why is it possible to call B b = new B() from class A, although the default constructor of B is private. In standard classes this would not be allowed, so I wonder why it's allowed to do this in inner classes.
Any input is appreciated.
Regards,
Tom
[1107 byte] By [
de_toma] at [2007-10-2 5:34:43]

Thanks for your reply. It seems, that this behavior is actually intended but can you please explain the reason for this. This would also imply that public/private visibility in inner classes is irrelevant. Is this correct? And if so, why?Yours clueless,Tom
> Thanks for your reply. It seems, that this behavior
> is actually intended but can you please explain the
> reason for this. This would also imply that
> public/private visibility in inner classes is
> irrelevant. Is this correct? And if so, why?
Presume that class B was public.
How would you prevent class C from creating an instance of class B? What access modifier would you put on the constructor that would prevent C from using it if not private?
It might help to keep in mind that inner classes are nothing more than a syntax convienence. There is nothing special about them in terms of the actual relationship to other classes including the container in terms of actual java code. That 'magic' that seems to happen is implemented by the compiler adding stuff to a normal class that makes it possible (and if you know what it does makes it possible to access it from other classes as well.)