Java: Non-static nested classes and instance.super()

Posted by Kiv on Stack Overflow See other posts from Stack Overflow or by Kiv
Published on 2010-05-14T02:19:19Z Indexed on 2010/05/14 2:24 UTC
Read the original article Hit count: 298

I'm having a hard time wrapping my head around non-static nested classes in Java. Consider the following example, which prints "Inner" and then "Child".

class Outer {
    class Inner {
        Inner() { System.out.println("Inner"); }
    }
}

public class Child extends Outer.Inner {
    Child(Outer o) {
        o.super();
        System.out.println("Child");
    }
    public static void main(String args[]) {
        new Child(new Outer());
    }
}

I understand that instances of Inner always have to be associated with an Outer instance, and that that applies to Child too since it extends Inner. My question is what the o.super() syntax means - why does it call the Inner constructor?

I've only seen a plain super(args) used to call the superclass constructor and super.method() to call the superclass version of an overridden method, but never something of the form instance.super().

© Stack Overflow or respective owner

Related posts about java

Related posts about inner-classes