How can "this" of the outer class be accessed from an inner class?

llm picture llm · Apr 28, 2010 · Viewed 22.4k times · Source

Is it possible to get a reference to this from within a Java inner class?

i.e.

class Outer {

  void aMethod() {

    NewClass newClass = new NewClass() {
      void bMethod() {
        // How to I get access to "this" (pointing to outer) from here?
      }
    };
  }
}

Answer

Guillaume picture Guillaume · Apr 28, 2010

You can access the instance of the outer class like this:

Outer.this