Can an outer class access the members of inner cla

2019-01-24 12:47发布

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?

5条回答
萌系小妹纸
2楼-- · 2019-01-24 13:26

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();
  }
}
查看更多
▲ chillily
3楼-- · 2019-01-24 13:31

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)

查看更多
forever°为你锁心
4楼-- · 2019-01-24 13:34

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.

查看更多
趁早两清
5楼-- · 2019-01-24 13:35

"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 =).

查看更多
干净又极端
6楼-- · 2019-01-24 13:39

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.

查看更多
登录 后发表回答