Consider the following case:
public class A {
public A() { b = new B(); }
B b;
private class B { }
}
From a warning in Eclipse I quot
I know this question is now almost three years old, but I find that a part of the question is still not answered:
And: does it mean that class B is no longer private at run time?
Carlos Heubergers comment on skaffmans answer suggests, class B
is still private
for other classes in the package.
He is probably right for the Java programming language, i.e. it is not possible to refer to class B
from an other class. At least not without using reflection (with which also private class members may be accessed from the outside), but this is another issue.
But as the JVM does not have any concept of an inner class (as skaffman states), I asked myself how an "accessible by only one class" visibility is realized at the bytecode level. The answer: It isn't realized at all, for the JVM the inner class looks like a normal package private class. This is, if you write bytecode for yourself (or modify one generated by the compiler) you can access class B
without problems.
You can access all synthetic accessor methods from all classes in the same package, too. So if you assign a value to a private field of class A
in a method of class B
, a synthetic accessor method with default (i.e. package private) visibility is generated in class A
(named something like access$000
) that sets the value for you. This method is supposed to only be called from class B
(and indeed it can only be called from there using the Java language). But from the JVMs point of view, this is just a method as any other and can be called by any class.
So, to answer the question:
B
is and stays private.B
(or better: class A$B
) is not private.