This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
When i compile & run Demo.java program
it won't encourage a compile error at line 10.
and prints 0 as value of static variable a.
I thought static initializer & object initializer(two programs of JVM) execute tasks to following order
01. initialize static variable(s) of super class
02. execute static block(s) of super class <<< pay attention
03. initialize static variable(s) of sub class <<< pay attention 04. execute static block(s) of sub class
05. initialize instance(non-static) variable(s) of super class
06. execute instance block(s) of super class
07. execute constructor method of super class
08. initialize instance(non-static) variable(s) of sub class
09. execute instance block(s) of sub class
10. execute constructor method of sub class
Supun Lakshan Dissanayake wrote:When i compile & run Demo.java program
it won't encourage a compile error at line 10.
and prints 0 as value of static variable a.
I thought static initializer & object initializer(two programs of JVM) execute tasks to following order
01. initialize static variable(s) of super class
02. execute static block(s) of super class <<< pay attention
03. initialize static variable(s) of sub class <<< pay attention 04. execute static block(s) of sub class
05. initialize instance(non-static) variable(s) of super class
06. execute instance block(s) of super class
07. execute constructor method of super class
08. initialize instance(non-static) variable(s) of sub class
09. execute instance block(s) of sub class
10. execute constructor method of sub class
should i switch tasks 2 and 3 ?
Regards!
Not sure what you are trying to say, but you marked the two lines with "pay attention" like it is supposed to surprised us.... The order is correct (not quite, but good enough), and because of that order the output should be zero (or it should, sometimes class loading surprises me). Based on the order what did you think the output should be?
I thought static initializer & object initializer(two programs of JVM) execute tasks to following order
01. initialize static variable(s) of super class
02. execute static block(s) of super class
To be exact, initialization of static variables and executions of static initializers are done at the same time -- and they are done in the order that they are encountered in the code. The same goes for initialization of instance variables and instance initializers. The reason it is like that in your example, is because that is the order in your code.