ggrr
ggrr

Reputation: 7867

How to access outer outer class(not outer class) property?

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.

Upvotes: 1

Views: 307

Answers (2)

Jabir
Jabir

Reputation: 2866

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

Upvotes: 1

Codebender
Codebender

Reputation: 14471

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

Upvotes: 2

Related Questions