Use OuterClass.this.x
; OuterClass.this
references the parent object (from whom the InnerClass
object is spawned).
Consider the following overshadowing example posted in the official Oracle tutorial:
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);
}
}
with the output
x = 23
this.x = 1
ShadowTest.this.x = 0
x = 23
refers to the local variable of the method void methodInFirstLevel()
, this.x = 1
refers to the public field x
of the FirstLevel
inner-class, and ShadowTest.this.x = 0
refers to the public field x
of the ShadowTest
outer-class.