stan freman wrote:I am not sure why. My understanding is the program starts from the main method, so only 3 should be printed. Apparently I am wrong, can anyone explain the correct order of running this code?
Prathima gaitonde wrote:I always had a doubt in my mind whether a instance variable initialization is done first or the constructor finishes first
Prathima gaitonde wrote:Hi Roel, there is no need for me to have the doubt about static and non static initializer block, especially getting support from people like you and others on this site.
Prathima gaitonde wrote:1> static initializer block gets executed even before the class gets initialized, i.e its objects getting created, and it is done only once when the class is loaded
Prathima gaitonde wrote:2> When will the class get loaded is explained several times on this site, cant remember the exact thread.
Prathima gaitonde wrote:4> If we add new B(); in main method now
a> default constructor of B() is called,
b> default constructor of A() is called,
c> All the instance initializer blocks of A() get executed
d> Constructor of A() finishes
e> All the instance variables get there explicitly initialized values(if any)
and same step from c to e for class B() will be executed.
And a post of mine would not be complete if it didn't have a pop quiz questionSo here it is: what will happen if instance variable var is made final? Does it still compile? And if it does, what's the output?
Prathima gaitonde wrote:I am not perfect, and still trying to climb the ladder, not even on the first step, so sorry for the mistakes made in my last posts
Prathima gaitonde wrote:I hope, have answered this without any mistakes,
Prathima gaitonde wrote:4> Variable declared as only final not static are instance variable which are constants, whose assignment can be postponed till constructor finishes.
Roel De Nijs wrote:And a post of mine would not be complete if it didn't have a pop quiz question
So here it is: what will happen if instance variable var is made final? Does it still compile? And if it does, what's the output?
Hope it helps!
Kind regards,
Roel
Daniel Andres wrote:I think it still runs because while it is not initialized on line 9 it is on line 11. Is this correct?