What if I made an explicit reference to 'this' for use inside an inner class?

Posted by badp on Stack Overflow See other posts from Stack Overflow or by badp
Published on 2011-02-03T15:14:44Z Indexed on 2011/02/03 15:26 UTC
Read the original article Hit count: 203

Filed under:
|

So far, I've used this approach to access this from the scope of an inner class:

class FooManagementWindow extends JFrame {
  JButton rejectFoo;
  //...
  void getFooAcceptingPanel(){
    //...
    final FooManagementWindow referenceToThis = this;
    rejectFoo = new JButton("Reject");
    rejectFoo.addEventListener(new EventListener() {

      @Override
      public void actionPerformed(ActionEvent arg) {
        referenceToThis.setEnabled(false); //this requires a network call
        //...
        referenceToThis.setEnabled(true); //the user may resume his work
      }
    });                
    //...
  }
}

However, I just learned that instead of declaring referenceToThis, a direct reference is kept for me as:

FooManagementWindow.this

I have no reason to think my less standard approach may lead to errors or weird corner cases. Or are there?

© Stack Overflow or respective owner

Related posts about java

Related posts about inner-classes