Bookmark Topic Watch Topic
  • New Topic

Am i gone crazy???

 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Report post to moderator
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!
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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?

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Supun Lakshan Dissanayake wrote:

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.

Henry
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
Supun, you already have a thread for this question. Please continue there.
http://www.coderanch.com/t/601280/java/java/runs-constructor-class
 
    Bookmark Topic Watch Topic
  • New Topic