开发者

Accessing the shadowed field from enclosed member class (Java)

开发者 https://www.devze.com 2022-12-23 16:52 出处:网络
is it possible to access a shadowed field of an enclosing class from the enclosed one in Java? public class Inherit {

is it possible to access a shadowed field of an enclosing class from the enclosed one in Java?

public class Inherit {    

    public int a = 3;
    private int b = 5;
    public class Inheriting {
        public int a = 23;
        private int d = 8;
        public void f() {
            System.out.println("Here I want to get a = 3");
          开发者_开发知识库  ...
        }
    }
}


public void f() {
    System.out.println("Here I want to get a = 3" + Inherit.this.a); 
}


Yes,

Inherit.this.a;

But you'd better choose more descriptive names so that they don't clash.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号