Bruce
Bruce

Reputation: 65

Accessing the shadowed field from enclosed member class (Java)

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");
            ...
        }
    }
}

Upvotes: 1

Views: 155

Answers (2)

Roman
Roman

Reputation: 66196

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

Upvotes: 2

Bozho
Bozho

Reputation: 597324

Yes,

Inherit.this.a;

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

Upvotes: 1

Related Questions