Java: Calling method of outer class from inner class

You use the syntax OuterClass.this.method(). Here's an example:

class OuterClass {
    class Inner {
        void test() {
            method();                  // "Inner"
            OuterClass.this.method();  // "Outer"
        }

        void method() {
            System.out.println("Inner");
        }
    }

    public void method() {
        System.out.println("Outer");
    }
}

See also: Calling super method of outer class from inner class

Comments

Be the first to comment!