Can an outer class access the members of inner class?

左心房为你撑大大i 提交于 2019-12-18 04:05:42

问题


The inner class is the class defined inside a class, and the inner class can be declared as public, private, protected. If the inner class defined as private and protected, can outer class access the members of inner class? and can inner class access members of outer class?


回答1:


If the inner class defined as private and protected, can outer class access the members of inner class?

Yes. These qualifiers will only affect the visibility of the inner class in classes that derive from the outer class.

Can inner class access members of outer class?

Yes, including the ones declared private, just as any instance method can.




回答2:


In general, you can (access private fields on inner classes and vice-versa). The following code compiles under Eclipse:

public class Outer {

  private int x;

  public void f() {
    Inner inner = new Inner();
    inner.g();
    inner.y = 5;
  }

  private class Inner {
    private int y;

    public void g() { x = 5; }
  }    
}

That said, you can configure your IDE/compiler to treat accesses to such fields as errors (in Eclipse this setting is called "Access to non-accessible member of an enclosing type", under Preferences -> Java -> Compiler -> Error/Warnings -> Code Style)




回答3:


"A nested class is a class defined within another class. A nested class should exist only to serve its enclosing class. If a nested class would be useful in some other context, then it should be a top-level class. There are four kinds of nested classes: static member classes, nonstatic member classes, anonymous classes, and local classes. All but the first kind are known as inner classes."

(Joshua Bloch, from the book Effective Java.)

As for your questions: it is very easy to test by yourself. But the answer is yes (even for private members), as long as you are not trying to access a non-static member (other than from a reference) from a static context, or trying to access a member which is in an inaccessible scope.

That is, very much as one would expect =).




回答4:


Yes! You can access both an inner class member from outer class, and vice-versa(irrespective of the access modifier). However, for a static nested class you cannot access its field just by the field name, and you need to access it like

InnerClass.staticInnerField 

though you can access the static fields of the outer class from the inner class directly by the fields names.




回答5:


Explanation is in context of regular inner class[Regular inner classes cannot have static members declared inside them]

You can access any field of outer class from inner class directly.

class Outer {
  private static int x = 0;

  class Inner {
    void print() {
      System.out.println(x); // x can be directly accessed
    } 
  }

  public static void main(String[] args) {
    new Outer().new Inner().print();
  }
}

Even Outer class can access any field of Inner class but through object of inner class.

class Outer {
  private class Inner {
    private int x = 10;
  }

  void print() {
    Inner inner = new Inner();
    System.out.println(inner.x);
  }

  public static void main(String[] args) {
    Outer outer = new Outer();
    outer.print();
  }
}


来源:https://stackoverflow.com/questions/5770575/can-an-outer-class-access-the-members-of-inner-class

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