(Editing: added in response to answer some comments)
The compiler takes inner classes and turns them into top-level classes. Since private methods are only available to the inner class, the compiler must add new โsyntheticโ methods that have access to the package level so that top-level classes have access to it.
Something like this (added by the $ compiler):
class A { private void f() { final B b; b = new B();
Non-static classes are the same, but they have the addition of a reference to an external class so that methods can be called on it.
The reason Java does this is because they do not want to require changes to the VM to support the inner classes, so all the changes had to be at the compiler level.
The compiler takes the inner class and turns it into a top-level class (thus, at the VM level there is no such thing as an inner class). Then the compiler should generate new methods of "transfer". They are created at the package level (not publicly available) to provide access to them only for classes in one package. The compiler has also updated method calls for private methods for generated forwarding methods.
You can prevent the compiler from generating a method that declares methods as a โpackageโ (lack of public, private, and protected). The disadvantage of this is that any package class can call methods.
TofuBeer Mar 19 '09 at 17:24 2009-03-19 17:24
source share