How to access outer class method variable in local inner class if it was shadowed there?

时间:2017-08-05 10:27:08

标签: java

How can I access variable int a=89; of Outer class inside my Inner class? Check my code. Help me to solve this.

public class TestOuter {
    private String name = "Makky";
    int a = 1;

    public void dis() {
        System.out.println("dis");
        int a = 89;
        class TestInner {
            int a = 6;

            void dis() {
                int a = 12;
                System.out.println("local inner class=" + a);
                System.out.println("local inner class=" + this.a);
            }
        }
        TestLocalInner ob = new TestLocalInner();
        ob.dis();
    }

    private class TestInner {
        int a = 2;

        public void access() {
            int a = 3;
            System.out.println("a=" + a);
            System.out.println("name=" + name);
            System.out.println("a=" + this.a);
            dis();
        }
    }

    public static void main(String[] args) {
        TestOuter.TestInner inner = new TestOuter().new TestInner();
        inner.access();
        TestOuter outer = new TestOuter();
        System.out.println(outer.a);
    }
}

Here I want to access variable int a = 89 of dis() method in my Inner Class without print value of a=89 inside that dis method like System.out.println(a);

1 个答案:

答案 0 :(得分:0)

TestInner成员:this.a
TestInner.dis本地变量:a
TestOuter成员:TestOuter.a