0

Possible Duplicate:
Keyword for the outer class from an anonymous inner class?

My class (let's call it MyClass) has m_listener member that is used for notification purposes. There's no problem to use it from within an anonymous method:

private void myMethod(SomeObj myObj)
{
    ...
    myObj.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            m_listener.myOnClick(this);
            return false;
        }
    });
    ...
}

In the code this refers to the anonymous OnTouchListener instance. What should I write instead of this to refer to MyClass instance (just like m_listener refers to MyClass.m_listener, not to the OnTouchListener.m_listener)?

Community
  • 1
  • 1
Nick
  • 2,905
  • 8
  • 55
  • 100

2 Answers2

4

Use MyClass.this to refer to the outer MyClass instance.

Paul Bellora
  • 53,024
  • 17
  • 128
  • 180
  • 1
    It saddens me that I get more rep from trivial answers like this than more interesting ones like [this](http://stackoverflow.com/a/13214630/697449). – Paul Bellora Nov 06 '12 at 15:39
1

To refer to MyClass instance use MyClass.this

zvez
  • 818
  • 5
  • 8