Pinki Roy wrote:
I don't understand why the out put is
As per my understanding it should have been
Someone please clarify please.
Alex Theedom wrote:The order in which the parts of the code are executed after the new InitDemo() is as follows:
1. When the "new InitDemo()" is executed a new object is being constructed so the constructor in line 8 is called,
2. this constructor then calls the super constructor in the A class, line 2;
3. the super constructor completes and returns control to the InitDemo class in which the instance variable is initialised,
4. a new A class is constructed in line 7 and the constructor of the A class is called again,
5. then the control passes to line 9 and the rest of the construtor of class InitDemo is executed.
It is true that the control jumps around a bit and can make you feel a bit dizzy at first, but there is a strict order of execution that is followed each time a class is instantiated. Also dont forget to included initialiation blocks (static and non static) in the execution order, they make it really good fun.
Instance blocks can give value to instance variables;
Instance blocks can use instance variables, if they are defined before the instance block in the code.