Reputation: 440
interface Interf {
void m1();
}
public class Test {
int x = 888;
Interf f;
public void m2() {
Integer x = 777;
f = new Interf() {
Integer x = 666;
public void m1() {
int x = 555;
System.out.println(x);
System.out.println(this.x);
}
};
}
public static void main(String[] args) {
Test t = new Test();
t.m2();
t.f.m1();
}
}
How can I access x variable with value 777 inside m1() method(In Anonymous class) with same name? Is it possible to access?
Upvotes: 0
Views: 52
Reputation: 201439
No, because it is shadowed. But you can change the name (and no need for Integer
, int
will suffice).
public void m2() {
int y = 777;
f = new Interf() {
int x = 666;
public void m1() {
int x = 555;
System.out.println(x);
System.out.println(this.x);
System.out.println(y);
}
};
}
Outputs
555
666
777
Upvotes: 2