Domain initialization, static blocks, and constructing methods, etc., execute the order when creating class instances

zhaozj2021-02-16  51

In Chapter 4, "Objects and Classes" in "Core Java 2: Volumn 1, Edition 5", "Objects, Static Blocks, and Construction Methods, etc., in the execution order, Chinese translation when creating class instances Some translation is not appropriate, and there is also a mistake in the English original book. This article uses a small program to explain the statement execution order in the class instance constructor.

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 Be 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 Reference from a static context / / System.out.println (d); // Non-static variable c cannot be reference from a static context s_a = 11111; s_b = 22222; // c = 33333; // non-static variable c cannot be Reference from a static context // D = 44444; // non-static variable c cannot be referened from a static context system.out.println (s_a); system.out.println (s_b); // system.out.println (C ); // Non-static variable c cannot be reference from a static context // system.out.println (d); // Non-static variable c cannot be referened from a static context system.out.println ("Before New Class 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 reference from a static context // system.out.println (d);

// Non-static variable c cannot BE Reference from a static context s_a = 111111; s_b = 222222; // c = 333333; // Non-static variable c cannot be reference from a static context // D = 444444; // Non-static variable c cannot be referened from a static context system.out.println (s_a); system.out.println (s_b); // system.out.println (c); // Non-static variable c cannot be Reference from a static context // system.out.println (d); // Non-static variable c cannot be referened 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 Be Reference from a static context // system.out.println (d); // Non-static variable c cannot BE Reference 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 reference from a static context // system.out.println (d); // non-static variable c cannot be reference from a static context system.out .println ("end static block");} int D = 4; static int S_B = 2;} Output is as follows:

begin static block111end static blockbegin main1121111122222before new class objectbegin block11111222223111222333end blockbegin second constructorend second constructorbegin constructor11122233341111222233334444end constructorend new class object11112222111111222222end main output by analysis, the following results: 1, the first class loading time, performs static fields (field) Initialize the schematic and static blocks (partially included with static {}). Here to pay attention to: a, no matter where the actual location of the static domain declaration statement, when the first load class is loaded, it will initialize it to the default value (0, False, NULL, etc.). b, even if the static domain declaration uses an explicit initialization statement (such as: int x = 3), the first load is loaded into the default value (this time X is 0), then follow The key c on the following is to perform the assignment statement (x = 3). C, the explicit initialization statement and static block of the static domain, perform in the order in which the code in the class is executed. Therefore, in the above example program, we see static int s_a = 1; static {s_a = 11; s_b = 22;} static int S_B = 2; there will be different effects on S_A, S_B. When the class is loaded, S_A, S_B are initialized to 0, and then S_A = 1 is performed in order in accordance with the code order; S_A = 11; S_A = 11; the results S_A, S_B becomes 11 and 2, respectively.

2. When constructing a class instance, the instance field is initialized to the default value, then perform the instance block (part of the {}), then perform the constructor. Where: a, as in 1, if there is an explicit initialization statement of the instance field, the program is still initializing the domain to the default value, and then performs the initialization statement or instance block according to the order that appears in the class. If the instance block location is in front of the initialization statement, even if it changed the value of the domain, it will be changed back by the initialization statement. B. After entering the construction method, if the first sentence of the construction method is to call another configuration method using this (...), another configuration method is performed first, and then the method of the present configuration method is performed. This usage must make this (...) in the first sentence.

"Core Java 2" book "After entering the construction method, if the first sentence is to call other construction methods, enter the other constructor. Otherwise, the method of implementing instance blocks" is problematic. The fact is that the instance block is first performed regardless of using this (), and then enter the construction method. In addition, the program needs to be compiled under SDK1.4, compiling under SDK1.3 will not allow the position of the position in which the positions declared later in them are not allowed.

转载请注明原文地址:https://www.9cbs.com/read-23973.html

New Post(0)