How to get reference to an anonymous object 2 levels up

I have code like this:

TextBox txt = new TextBox(){
  public void onLoad(){
    this.addFocusHandler(new FocusHandler(){
      //some codes here
      //if I use "this" keyword, it refers to the handler, but how can I get a reference to the textbox?
    });
  }
};

Question is embedded in the position.


Edit:

In respect to the answers, the creation of a pre-defined reference works for this situation, but this apparently lost (or at least reduce) the benefits of anonymous object/function.

I hope to find a way without creating a new reference. Rather just to get the reference from that scope.


After all the answers, here is a conclusion:

  • Reflection does not work in GWT. (at least I did not succeed) obj.getClass() works, but others like getMethods() or getEnclosingClass() don't work.
  • The way to get a reference can either be declaring a reference in the right scope, or get a higher level object reference and reference downwards. I prefer the latter simply because you don't need to create a new variable.
Answer:1

The enclosing instance of a non-static inner class (anonymous or named) in Java is available as ClassName.this, i.e.

TextBox txt = new TextBox(){
  public void onLoad(){
    this.addFocusHandler(new FocusHandler(){
      doSomethingCleverWith(TextBox.this);
    });
  }
};
Answer:2

This may work for you:

TextBox txt = new TextBox(){
    public void onLoad(){
        final TextBox ref = this;
        this.addFocusHandler(new FocusHandler(){

            public void doSomething(){ 
                //some codes
                ref.execute();
            }
        });
    }
};

But I prefer to migrate inner classes to named classes:

public class Test {

    public void demo(){
        TextBox txt = new TextBox(){
            public void onLoad(){
                this.addFocusHandler(new DemoFocusHandler(this));
            }
        };
    }
}

External FocusHandler:

public class DemoFocusHandler extends FocusHandler {

    private TextBox textBox;

    public DemoFocusHandler(TextBox textBox){
        this.textBox = textBox;
    }

    public void doSomething(){ 
        //some codes
        textBox.execute();
    }
}
Answer:3

I want the functionality of the Stanford Core NLP, written in java, to be available in C++. To do this I am making use of the Java Native Interface. I have a Java object that wraps multiple functions ...

I am successfully parsing and evaluating a javascript file with Duktape in my Android application using Kotlin. val file_name = "lib.js" val js_string = application.assets.open(file_name)....

I'm using this class as my key to Hashmap with overriden hasCode() and equals() public class Design { private double[] factors = null; public double[] getFactors() { return factors; } public ...

I didn't understand why I am getting empty list from criteria, and I have data in my table. code where I'm getting List: hibernateSession_destination = HibernateUtilReports.INSTANCE.getSession(); ...