简体   繁体   English

为什么我可以访问封闭类引用的私有成员

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

I have seen many questions about accessing private members of an enclosing class. 我已经看到很多关于访问封闭类的私人成员的问题。 However, my question is the opposite. 但是,我的问题恰恰相反。

If I have (as an example), the following code: 如果我(作为示例),以下代码:

public class A {

   private String outerString = "silly string";

   static class B {
      private final A someA = new A();      

      public void foo() {
         String b = someA.outerString ;
      }
   }
}

I'm wondering why this compiles? 我想知道为什么这个编译? I would have expected an error by virtue of the way in which I am accessing the 'outerString' instance variable from class A (via someA.outerString). 我希望通过我从类A(通过someA.outerString)访问'outerString'实例变量的方式来预测错误。 I know that an inner class can access the enclosing class members directly by an implicit 'this' reference. 我知道内部类可以通过隐式的“this”引用直接访问封闭的类成员。 But here, class B is static, so the 'this' reference won't apply. 但是在这里,B类是静态的,因此'this'引用不适用。

B is a member of A and therefore has access to A 's private fields and methods. BA的成员,因此可以访问Aprivate字段和方法。
In this case, although B is static it is using an instance of A to access the field A.outerString . 在这种情况下,尽管Bstatic ,但它使用A的实例来访问字段A.outerString

static methods of a class can access private members of the same class through the same class instance. static方法可以通过同一个类实例访问同一个类的private成员。 This behavior is consistent for static classes as well. 这种行为对于static类也是一致的。

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. 1. this只是非静态成员的作品,这就是正确的 .....但你不使用 this ,但外部类的实例。

2. And you can very well access the Outer class private member from the (Top level) inner static class. 2.您可以从(顶级)内部静态类 访问 Outer class private member

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. 3.从外到内,从内到外有能力访问彼此的私有成员 .. 唯一不同的是, non static inner class具有对外部类的隐式引用 ,对于static inner class您必须使用实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 公共嵌套类的子类无法访问封闭类的私有成员 - Subclass of a public nested class is unable to access private members of enclosing class 在Java嵌套类中,封闭类可以访问内部类的私有成员吗? - In Java nested classes, can the enclosing class access private members of inner classes? 静态嵌套类可以访问封闭类的哪些成员? - What members of the enclosing class can a static nested class access? 为什么外部 Java 类可以访问内部类私有成员? - Why can outer Java classes access inner class private members? 为什么您可以访问和操纵班级的私人成员 - Why can you access and manipulate private Members of your own class 如何在Java中访问私有类成员? - How can I access private class members in Java? 在更高版本的 Java 中,内部类如何访问封闭类的私有成员? - How do inner class access enclosing class' private members in higher versions of Java? 在静态工厂方法中使用覆盖方法创建实例时,如何访问封闭类中的私有字段? - How can I access private field in enclosing class when creating instance with overridden method in static factory method? 封闭类成员的内部类访问 - inner class access of enclosing class members 封闭类型的静态嵌套子类仍然可以引用私有字段成员,为什么? - Static nested sub-classes of the enclosing type can still refer to the private field members, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM