Method Local Inner Class Member scope access

后端 未结 3 840
死守一世寂寞
死守一世寂寞 2021-01-07 08:15

How do I access the method variable having the same name as the inner class member instance or method local variable of the inner class?

    class A{
                


        
3条回答
  •  粉色の甜心
    2021-01-07 08:49

    that code will never output those statements, here is an example from Sun that should explain things :

        public class ShadowTest {
        public int x = 0;
    
        class FirstLevel {
    
            public int x = 1;
    
            void methodInFirstLevel(int x) {
                System.out.println("x = " + x);
                System.out.println("this.x = " + this.x);
                System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
            }
        }
    
        public static void main(String... args) {
            ShadowTest st = new ShadowTest();
            ShadowTest.FirstLevel fl = st.new FirstLevel();
            fl.methodInFirstLevel(23);
        }
    }
    

提交回复
热议问题