| Author |
static initializer
|
tony kanvas
Ranch Hand
Joined: Oct 26, 2002
Posts: 97
|
|
hi all //The program prints out "5" once. // why the program will not print 5 6 7 and we know static will execute first then the do the list I mean the constructor thanks in advs.
|
 |
Jay Ashar
Ranch Hand
Joined: Oct 13, 2002
Posts: 208
|
|
|
Static initializer will be executed when the class is loaded, once you create instance of that class only instance initializer and constructors are executed, if you create multiple instance of class instance initializer and constructors are executed multiple times but static initializer will not.
|
SCJP 1.4<br />SCWCD 1.3
|
 |
Mayuri Son
Greenhorn
Joined: Oct 31, 2002
Posts: 4
|
|
Correct me if I am wrong When a class is loaded, first all static members are initialized, then all static blocks are executed in the order of declaration. But when the class is instatiated then if this class is derived from some superclass then the instance initalizer(s) of superclass(es), then constructor(s) of the superclass(es) then instace initializer of the class and finally constructor of the class is executed. And as Hetal said static blocks are not executed at the time of instatiation only at the time of loading
|
 |
noel angel
Ranch Hand
Joined: Oct 27, 2002
Posts: 75
|
|
|
In other words....It is not that the value of si did not change. The static initialization block only gets executed one time. Since this is where you put the System.println call, it is only executed once.
|
 |
Yan Bai
Ranch Hand
Joined: Jul 21, 2002
Posts: 125
|
|
Originally posted by Mayuri Son: When a class is loaded, first all static members are initialized, then all static blocks are executed in the order of declaration.
I am wondering whether there's precedence between these two. I remember they are initialized sequencially or maybe I am wrong.
|
SCJP 1.4
|
 |
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
|
|
All static initialization is done in textual order. It does not matter whether you are initializing a static variable, or executing a static initialization block. For instance prints 1 2 3 4
|
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
|
 |
Mayuri Son
Greenhorn
Joined: Oct 31, 2002
Posts: 4
|
|
Thanks Yan,Ron for the correction.
|
 |
 |
|
|
subject: static initializer
|
|
|