Is it possible to explicitly access instance members of a nested class's containing class?

Go To StackoverFlow.com

1

Is there a keyword in java to allow explicit invocation of members of the containing instance (or of its superclass) from within a nested class?

SCENARIO

public class Superclass {
    public void doSomething() {
        // do something that's of interest to the superclass
    }
}

public class Subclass extends Superclass {
    @Override
    public void doSomething() {
        // do something that's of interest to the subclass
        // possibly call super.doSomething()
    }

    private class NestedClass {
        private void doSomething() {
            // do something that's of interest to the nested class
            // calling doSomething() here without an explicit scope
            // specifier results in a stack overflow
        }

        private void doSomethingElse() {
            if (somethingOfInterestToTheSubclassIsNotImportant) {
                // just invoke the superclass's doSomething() method
            } else {
                // invoke the subclass's doSomething() method
            }
        }
    }
}
2012-04-05 18:51
by arootbeer


2

Oh, totally. Just use Subclass.this.doSomething(), or Subclass.super.doSomething() to get the superclass method.

2012-04-05 18:52
by Louis Wasserman
Perfect! Please add Subclass.super.doSomething() and I'll accept your answer - arootbeer 2012-04-05 19:11
...Huh. I did not know you could do that - Louis Wasserman 2012-04-05 19:15
Ads