Monday, June 13, 2011

An example where super() doesn't have to be the first statement in a constructor?

Java requires that if you call super() in a constructor, it must be the first statement. Do you know any place where we can call super() after some statement in a constructor?

Actually, When you construct a inner class within a non-static context, such as in a non-static method, a constructor or an initializer block, the class always has a pointer to the outer object. If you check the decompiled version of classes, the anonymous class sets the local field before calling super(). This is the only place where this is allowed in Java.

Please check this link for more details.

No comments: