This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Am i gone crazy??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Am i gone crazy???

Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132

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!


Are you better than me? Then please show me my mistakes..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

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

Joined: May 03, 2008
Posts: 4523
    
    5

Supun, you already have a thread for this question. Please continue there.
http://www.coderanch.com/t/601280/java/java/runs-constructor-class


luck, db
There are no new questions, but there may be new answers.
 
wood burning stoves
 
subject: Am i gone crazy???
 
Similar Threads
initializers
order?
Object Initialization
Regarding static blocks
Why first of all it runs constructor of class B?