I found the following question on Dan's mock on constructors: Suppose that the superclass constructor invocation statement, super, appears explicitly in a subclass constructor. If a compile-time error is to be avoided then the arguments for the superclass constructor invocation statement, super, can not refer to which of the following? a. Static variables declared in this class or any superclass. c. Static methods declared in this class or any superclass. The above were among the valid answers. Is this possible because all classes in the hierarchy are loaded initially and then the constructors invoked hierarchially ? My understanding of object initialization is: 1. super or this is invoked 2. static variables and static methods are executed in textual order. 3. instance variables and instance methods are executed in textual order. 4. rest of the constructor is executed. Is the above sequence correct? Thanks.
I couldn't find this question, can you post a link to it? Also: 6-Instance Member Variables can NOT be referenced in a constructor before the call to super() returns. So super(x) where x is an instance variable will give a compile time error. This I'm sure of.. about static variables, I don't think these 2 answers are correct but maybe there's something Dan knows that's not clear to me, that's why I wanted to see his comment on the question in the answers he offer. Also: 1-On instantiating an object of a certain type, the static initializers of the super classes are executed first starting with the highest class in the heirarchy, then the static initializer of the class of the object itself, then because of an implicit or explicit super() call the instance initializers and the constructors of the super classes are executed in couples (instance initializer & constructor) then at the end the inistance initializer and constructor of the class of the object itself are executed. 2-If any static variables are declared in a class, they will be initialized during the loading of the class. Instance variables are initialized with the creation of a new object (instance) of the class. HTH
Vijay, The answers that I provide for that question are as follows:
Instance variables declared in this class or any superclass.
Instance methods declared in this class or any superclass. The keyword "this".
The keyword "super".
The remark associated with the question is as follows.
The superclass constructor invocation statement, super, is processed before the new instance of the class has been created. For that reason, the arguments to the superclass constructor invocation statement can not legally refer to members that have not been created and initialized.
Please note that question asks
If a compile-time error is to be avoided then the arguments for the superclass constructor invocation statement, super, can not refer to which of the following?
The reader is required to select the true statements only. The true statements were the ones that I posted earlier in this response. The reader should assume that any answer that is not listed as "true" is threrefore "false". The two answer options that you have posted were the two answer options that are not among the set of true answers and therefore should be assumed false.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>