In the fourth chapter of the "Core Java 2:volumn 1, Edition 5", "Objects and Classes", the order of execution of domain assignment statements, instance blocks, static blocks, and construction methods in the creation of class instances is discussed in the Chinese version, and there is a mistake in the English original book. In this article, a small program is described to illustrate the order of statement execution during class instance construction.
The procedure is as follows :
public class teststaticblock{Public Teststaticblock () {This ("second"); System.out.println ("begin constructor"); System.out.println (s_a); System.out.println (S_b); System.out.println (c); System.out.println (d); This ("second"),//call to this must is first statement in constructor s_a=1111; s_b=2222; c=3333; d=4444; System.out.println (s_a); System.out.println (S_b); System.out.println (c); System.out.println (d); System.out.println ("End constructor"); } public Teststaticblock (String s) {System.out.println ("Begin second Constructor"); System.out.println ("End second constructor"); } public static void Main (String args[]) {SYSTEM.OUT.PRINTLN ("begin main"); System.out.println (s_a); System.out.println (S_b); System.out.println (c);//non-static variable c cannot be referenced from a static context//System.out.println (d);//no N-static variable c cannot be referenced from a static context s_a=11111; s_b=22222;//c=33333;//non-static variable c cannot be referenced from a static context//d=44444;//non-static variable c cannot is referenced from a static context System.out.println (S_A); System.out.println (s_b);//System.out.println (c);//non-static variable c cannot be referenced from a static context//Sy Stem.out.println (d);//non-static variable c cannot be referenced from a static context System.out.println ("before new CLA SS Object "); Teststaticblock t = new Teststaticblock (); System.out.println ("End New Class object"); System.out.println (s_a); System.out.println (s_b);//System.out.println (c);//non-static variable c cannot be referenced from a static context//Sy Stem.out.println (d);//non-static variable c cannot be referenced from a static context s_a=111111; s_b=222222;//c=333333;//non-static variable c cannot be referenced from a static context//d=444444;//non-static Variab Le c cannot is referenced from a static context System.out.println (S_A); System.Out.println (s_b);//System.out.println (c);//non-static variable c cannot be referenced from a static context//System.ou T.println (d);//non-static variable c cannot be referenced from a static context System.out.println ("End Main"); } static int s_a=1; int c=3; {System.out.println ("begin Block"); System.out.println (s_a); System.out.println (S_b); System.out.println (c);//System.out.println (d);//illegal forward reference s_a=111; s_b=222; c=333; d=444; System.out.println (s_a); System.out.println (S_b); System.out.println (c);//System.out.println (d);//illegal forward reference System.out.println ("End Block"); } static {System.out.println ("Begin static Block"); System.out.println (s_a);//System.out.println (s_b);//illegal forward reference//System.out.println (c);// Non-static variable c cannot is referenced from a static context//System.out.println (d);//non-static variable C cannot b E referenced from a static context s_a=11; s_b=22; System.out.println (s_a);//SyStem.out.println (s_b);//illegal forward reference//System.out.println (c);//non-static variable C cannot be referenced From a static context//System.out.println (d);//non-static variable c cannot is referenced from a static context System. OUT.PRINTLN ("End static Block"); } int d=4; static int s_b=2; }
The output is as follows:
Begin static Block
1
11
End Static Block
Begin Main
11
2
11111
22222
Before new class object
Begin Block
11111
22222
3
111
222
333
End Block
Begin second Constructor
End second constructor
Begin constructor
111
222
333
4
1111
2222
3333
4444
End constructor
End New Class object
1111
2222
111111
222222
End Main
By analyzing the output, the following results can be obtained:
1.At the first load of the class, static domain (field) initialization statements and static blocks (with static{} included) are executed.
Here to note:
A,Regardless of the actual location of the static domain declaration statement, the first time the class is loaded, it is first initialized to the default value (0,false,null, etc.).
B,Even if an explicit initialization statement is used in the static domain declaration (for example: int x=3), the first time the class is loaded, it is initialized to the default value (at this point x is 0), and then the assignment statement (x=3) is executed according to the key point C below.
C,Explicit initialization statements and static blocks for static domains are performed in the order in which the code appears in the class.
So, in the above example program, we see
static int s_a=1;
Static
{
s_a=11;
s_b=22;
}
static int s_b=2;
will have a different effect on the s_a,s_b. When the class is loaded, the S_a,s_b are initialized to 0, and then the s_a=1;s_a=11;s_b=22;s_b=2 is executed in code order, and the result s_a, S_b respectively, becomes 11 and 2.
2 . When a class instance is constructed, the instance domain is initialized to the default value, then the instance block (the part enclosed in {}) is executed, and then the construction method is executed. which
A, as in 1, if there is an explicit initialization statement for the instance domain, the program will still initialize the domain to the default value, and then execute the initialization statement or instance block in the order in which the code appears in the class. If the instance block position is in front of the initialization statement, even if it changes the value of the field, it will be changed back by the initialization statement that was subsequently executed.
B, after entering the construction method, if the first sentence of the constructor method is to use this (...) If another constructor method is called, then another constructor is executed before the method body of this constructor method is executed. This usage must let this (...) In the first sentence.
In the Core Java 2 book, "After entering the construction method, if the first sentence is to invoke a different constructor method, then enter another constructor." Otherwise, the execution instance block "has a problem with the reference. The fact is that the instance block is executed first, regardless of whether you use this (), and then into the construction method. In addition, the program needs to be compiled under sdk1.4, and compiling under sdk1.3 will not allow the values of the fields declared behind them to be changed in a static block or instance block.
Original: http://www.moon-soft.com/doc/14364.htm
Order of execution of domain initialization, static blocks, and construction methods when creating class instances