1

For example,if A has inner class B,B has inner class C,all have a property "name",I know C can access name in B by B.this.name,but how to access name in A from C?

public class A{
    String name="A";
    public class B{
        String name="B";
        public class C{
            String name="C";
            public C(){
                //how to print name in A?
                //System.out.println(B.A.name);
                //System.out.println(B.A.this.name);
                //System.out.println(B.this.A.name);
                //System.out.println(B.this.A.this.name);
            }
        }
        C c=new C();
    }
    B b=new B();
    public static void main(String[] args){
        new A();
    }
}

I tried so many syntax but they cannot compile,also when search java outer class,I found most of them are about outer class only, not outer outer class.

ggrr
  • 7,737
  • 5
  • 31
  • 53

2 Answers2

2

Use A.this.name to access the outer most class. Or any other class.

Codebender
  • 14,221
  • 7
  • 48
  • 85
1

Using System.out.println(A.this.name);

Jabir
  • 2,776
  • 1
  • 22
  • 31