Consider the following article from the JLS: §15.9.5.1 When the anonymous class extends an inner class - then for the implicit constructor of the anonymous class - following is the rule regarding the body of the implicit constructor:
The constructor body consists of an explicit constructor invocation (§8.8.7.1) of the form
o.super(...)
, whereo
is the first formal parameter of the constructor, and the actual arguments are the subsequent formal parameters of the constructor, in the order they were declared.
Following is what we understand from this-:
o
- is the instance of the class - that just encloses the super class of the anonymous class.- when we do
o.super(...)
we are actually invoking the super class of the enclosing instance.
Consider the following program:
class A_ {
public A_(Boolean st){}
public class B_ {
public B_(Integer a){}
public class C_ {
public C_(String str){}
}
}
}
//when creating the anonymous constructor out of C - in a separate class:
public class ThisInAnonymousTesting {
public static void main(String[] args) {
A_.B_.C_ member =
new A_(true)
.new B_(23)
.new C_("Hey"){
};
}
}
Now when we decompile the anonymous class, we get the following:
/**
=== Anonymous class Declaration
*/
import A_.B_;
import A_.B_.C_;
final class ThisInAnonymousTesting$1 extends C_ {
// - Rule : the 1st argument is the class instance which is the enclosing instance
// of the Super class(C in this case) - ie B
ThisInAnonymousTesting$1(B_ x0, String str) {
x0.getClass();
//But the super() invocation here is for the super class - not for the enclosing instance
super(x0, str);
}
}
Following are my questions:
- Why do we need to do
o.super(...)
- when we are already passing the initialized instance ofo
to the anonymous class constructor?- ie
o
gets created only when its super classes would have already been called. - The
super()
call in the constructor is clearly trying to instantiate the classC_
which is fine - as it is the super class for the current anonymous class.
- ie
- In the decompiled version, what is the need of
x0.getClass();
- I mean why does JVM need to do thegetClass()
?
Not sure if my interpretation of o.super()
clause correct?