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.

3
  • 1
    Why you are creating this much complexity any specific reason? Commented Aug 5, 2015 at 6:41
  • Pass "A" to "B" constructor and Pass "B" to "C" constructor if you really require these nested classes Commented Aug 5, 2015 at 6:42
  • @DiSaSteR, there's no inheritance here. super() will call Object class' constructor. Commented Aug 5, 2015 at 6:57

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.