In my memory, static block is execute when class file be loaded in JVM,hence static { x /= 5; }should be failed;but i run this code that got answer,so anyone can tell me the order of initialization?Thanks
Thank Folks who <b>Make Sense</b> here.<br />SCJP Platform 2
The JVM first executes static initializers for StaticStuff ancestors recursively. Then, executes static initializers for your StaticStuff. Later, executes the main() methodh so you can see the output with variable�s value computed.
Hi, Order of code execution 1.static variables initialization. 2.static initializer block execution. (in the order of declaration, if multiple blocks found) 3.constructor header ( super or this – implicit or explicit ) 4.instance variables initialization / instance initializer block(s) execution 5.rest of the code in the constructor Interfaces Clement
Originally posted by Clement Ng: Hi, Order of code execution 1.static variables initialization. 2.static initializer block execution. (in the order of declaration, if multiple blocks found) 3.constructor header ( super or this – implicit or explicit ) 4.instance variables initialization / instance initializer block(s) execution 5.rest of the code in the constructor Interfaces Clement
This order of execution is really helpful, I actually copy it down. But I have a question related to this. Is this statement true or false: "The default constructor initializes the instance variables declared in the class. " Looks like instance variables intitialized at the time constructor is executed(between step 3 and 5), but is it OK to say constructor initializes the instance variables??