Why can I access the private members of an enclosing class reference

杀马特。学长 韩版系。学妹 提交于 2019-12-02 00:13:49

B is a member of A and therefore has access to A's private fields and methods.
In this case, although B is static it is using an instance of A to access the field A.outerString.

static methods of a class can access private members of the same class through the same class instance. This behavior is consistent for static classes as well.

static void b(A someA) {
    String b = someA.outerString;
}

1. this only works with non-static member, thats right..... But you are not using this but instance of the Outer Class.

2. And you can very well access the Outer class private member from the (Top level) inner static class.

3. Outer to Inner and from Inner to Outer has the ability to access the private member of each other..only difference is that, non static inner class has implicit reference to the Outer class, and for static inner class you must access using the Instance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!