I got this question from ExamLab practice test 2
I am not able to understand the flow of the program
please provide some help on initializer blocks and initialization process when the class in loaded in the memory
The output is
F:\Java\Javaranch problems>java ExamLabBoard
SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2012
OCEJPAD [83%] September 6th, 2014
If you find any post useful, click the "plus one" sign on the right
Remember the principle "static code (and block) is executed in sequence from top to bottom". The only relevant lines of code (to the method call in main) are:
When you call the capture() method, the static door variable has a value of 1. Hence capture() returns 1 and this gets assigned to the static cam variable. After that whatever happens to door DOESN'T affect cam. It only affects door when the static block below is executed:
The above code simply changes the value of door, but remember, cam has already been assigned. So cam will have a valu of 1. Hence what you see in the output of the program.
The instance initializer block
is irrelevant in the static context, since no objects have been created. It doesn't impact the value of cam in any way.