The super-class can be referred to by the keyword, super
, and the current class can be referred to via, this
, but is there a way to refer to the class that contains the nested class/anonymous class?
Asked
Active
Viewed 211 times
2

cesar
- 8,944
- 12
- 46
- 59
-
possible duplicate of [keyword for the outer class from an anonymous inner class?](http://stackoverflow.com/questions/56974/keyword-for-the-outer-class-from-an-anonymous-inner-class) – aioobe Oct 17 '11 at 17:21
2 Answers
3
Yes. Like this:
public class OuterClass {
class InnerClass {
void method() {
// Refer to outer class instance
doSomething( OuterClass.this );
}
void doSomething(OuterClass outer) {
// ...
}
}
}
- See also: Access
this
from Java anonymous class
3
I think you're looking for this answer.
Basically, if your outer class is called Container you use this syntax:
Container.this.methodNameGoesHere();
That will ensure that you will be calling the outer class's methodNameGoesHere() method even if there is a duplicate method in your anonymous class.