I didn't know THAT!

Inner class can call master class private method!

The inner class specification says that a private method of a class B can be used by a class A, if B encloses A. That is, if A is a class defined within B, it can call B's private methods. See the example:

public class B {
 
    private void myMethod() {
        // this Is a Private Method Of ClassB
    }
 
    class A {
        A() {
            myMethod();
        }
    }
 
    public B() {
        A a = new A();
    }
 
    public static void main(String args[]) {
        B x = new B();
    }
 
}

Inner class A calls a private method myMethod() defined in class B. Since Java Virtual Machine has restrictions on calling private members from outside of their class, a special access method named access$0() is generated by the compiler and added internally to the class B, and access$0() calls myMethod().

JDK disassembler program shows: $ javap -c B

Method void access$0(B)
   0 aload_0
   1 invokespecial #7 
   4 return

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License