Keyword for the outer class from an anonymous inner class

JavaAnonymous Inner-Class

Java Problem Overview


In the following snippet:

public class a {
    public void otherMethod(){}
    public void doStuff(String str, InnerClass b){}
    public void method(a){
        doStuff("asd",
            new InnerClass(){
                public void innerMethod(){
                    otherMethod();
                }
            }
        );
    }
}

Is there a keyword to refer to the outer class from the inner class? Basically what I want to do is outer.otherMethod(), or something of the like, but can't seem to find anything.

Java Solutions


Solution 1 - Java

In general you use OuterClassName.this to refer to the enclosing instance of the outer class.

In your example that would be a.this.otherMethod()

Solution 2 - Java

OuterClassName.this.outerClassMethod();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshsteimerView Question on Stackoverflow
Solution 1 - JavaBill the LizardView Answer on Stackoverflow
Solution 2 - JavajjnguyView Answer on Stackoverflow