Calling outer class function from inner class

Java

Java Problem Overview


I have implemented a nested class in Java, and I need to call the outer class method from the inner class.

class Outer {
    void show() {
        System.out.println("outter show");
    }

    class Inner{
        void show() {
            System.out.println("inner show");
        }
    }
}

How can I call the Outer method show?

Java Solutions


Solution 1 - Java

You need to prefix the call by the outer class:

Outer.this.show();

Solution 2 - Java

This should do the trick:

Outer.Inner obj = new Outer().new Inner();
obj.show();

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
QuestionSanthoshView Question on Stackoverflow
Solution 1 - JavaGuillaumeView Answer on Stackoverflow
Solution 2 - Javavijay suryaView Answer on Stackoverflow