Java: getting the correct "this" context in an anonymous action handler

I noticed a piece of code that I looked at that the author used:

class MainClass
{
  protected int someVar = 1;

  private SomeClass someClass = new SomeClass(this, new SomeActionListener() {
    protected void onAction() {
      MainClass.this.someVar ++;
    }
  });

  public MainClass()
  {
  }
}

Notice how he used MainClass.thisto get the correct 'this' context to change the scope back to MainClass. I have never seen this before - can anyone explain?

+3
source share
1 answer

, . . this MainClass.this , . , , someClass static, MainClass.this.

+5

All Articles