If I write the following class:
public class Example {
int j;
int k;
public Example(int j, int k) {
j = j;
k = k;
To answer your follow-up question about this with method, the inner class mentioned by Srikanth is still a valid example of using this: (with method this time)
public class OuterClass
{
void f() {System.out.println("Outer f()");};
class InnerClass {
void f() {
System.out.println("Inner f()");
OuterClass.this.f();
}
}
}
You have the same situation with anonymous class:
You can refer to the outer class’s methods by:
MyOuterClass.this.yOuterInstanceMethod(),MyOuterClass.myOuterInstanceMethod(),- or simply
myOuterInstanceMethod()if there is no ambiguity.