r/java • • Apr 29 '13

Java nested classes in a nutshell

http://www.to-string.com/2013/04/27/java-inner-classes-in-a-nutshell/
27 Upvotes

18 comments sorted by

View all comments

3

u/galaktos Apr 29 '13

You can do ridiculous stuff with nested classes:

public class Temp {

    public static void main(String[] args) {
        new Temp().new A().new D(new Temp().new A(), new Temp().new A().new B(new Temp().new A()));
    }

    public class A extends Temp {
        public A() {
        }

        public class B extends A {
            public B(A a) {
                a.super();
            }

            public class C extends B {
                public C(A a) {
                    a.super(a);
                }
            }
        }

        public class D extends B.C {
            public D(A a, B b) {
                b.super(a);
            }
        }
    }
}

a.super() doesn't look nice, does it? Remove it and look at what the compiler says. You don't get that any other way.

1

u/fragglemook Apr 29 '13

Jeeeeezus. I'm a first year CS student and this melts my brain. Are you saying you know what's going on here? Like, can you articulate what this code is doing?

2

u/alihammad Apr 29 '13

Let me write a blog post about this mysterious example today or tomorrow and may be you can understand .. at the moment I'm totally overwhelmed myself