Consider you have the following class
public class OuterClass {
...
private static class InnerClass {
int foo;
int bar;
}
}
I think I’ve read somewhere (but not the official Java Tutorial) that if I would declare the static member classes attributes private, the compiler had to generate some sort of accessor methods so that the outer class can actually access the static member class’s (which is effectively a package-private top level class) attributes.
Any ideas on that?
Yes that’s true. At least for the Sun javac. Have a look at the following example:
It defines a
static int access$002(int)for setting the value, and astatic int access$000()for getting the value. The setter also returns the value, presumably to easily compilesomeVariable = InnerClass.foo = 5.At line 2 and at line 9 it calls the setter (
access$002) and getter (access$000) respectively.Note also that it only introduces these accesser methods if they are needed. The
barfield for instance, was never accessed from outside the class, thus the compiler only generated a getter/setter for thefoofield.