If it still doesn't make sense, search for "static block" and "initializer block."
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Joined: Apr 24, 2006
Thanks Marc. I understand this, but one confusing part to post this Q basically is: why "In main: Creating new Hawk..." is NOT displayed at the Beginning of the output? Please someone explain. Thnaks output: ------ First static block in Raptor. Second static block in Raptor. In main: Creating new Hawk... Initializer block in Bird. Bird constructor. Initializer block in Raptor. Raptor constructor. In main: Hawk created.
with an empty main, and the output was everything that was in the static blocks. So I think that static blocks wherever they are are executed before the main. ( Is that due to the fact that Java is both interpreted and compiled ?)
Originally posted by Sai Charan: ...one confusing part to post this Q basically is: why "In main: Creating new Hawk..." is NOT displayed at the Beginning of the output? ...
One thing that causes classes to be loaded is accessing a static member. And static blocks execute when the class is loaded.
In this case, Hawk's static method main is invoked from the command line, which loads the class Hawk along with its superclasses. This is why the static block in Raptor executes (at class load time) even before the body of Hawk's main method is executed.