Java initialization order (static variables, static initialization blocks, instance variables, instance initialization blocks, constructor methods)

Source: Internet
Author: User

1. Order of Execution 1.1. The order of initialization in a class

(static variable, static initialization block) = = (variable, initialization block, constructor).

1.2, two initialization order with inheritance relationship classes

The parent class (static variable, static initialization block) is the subclass (static variable, static initialization block) and the parent class (variables, initialization blocks, constructors) and subclasses (variables, initialization blocks, constructors).

Examples are as follows: (Results see note)

1 classA {2      PublicA () {3System.out.println ("Constructor A."));4     }5 6     {7System.out.println ("Instance Block A."));8     }9     Static {TenSystem.out.println ("Static Block A.")); One     } A  -      Public Static voidMain (string[] args) { -         NewA ();/* the * Static block A. Instance block A. Constructor a . -                  */ -     } - } +  - classBextendsA { +      PublicB () { ASystem.out.println ("Constructor B."); at     } -  -     { -System.out.println ("Instance Block B.")); -     } -     Static { inSystem.out.println ("Static Block B.")); -     } to  +      Public Static voidMain (string[] args) { -         NewA ();/* the * static block A. Static block B. Instance block A. Constructor A. *                  */ $ System.out.println ();Panax Notoginseng         NewB ();/* - * Instance block A. Constructor A. Instance block B. Constructor B. the                  *///static members and static initialization blocks are executed only once.  +     } A}
2, the impact on the value of the variable 2.1, the problem

How does the final value of a variable be determined when the initialization of a variable in Java and the assignment of the initialization block to the variable are present? In particular, initialization blocks are more confusing to the variables before they are defined, even when static initialization blocks and instance initialization blocks are assigned to them. For example, what are the output values? (given):

1 classC {2 3     Static {4A = 2;5b = 2;6     }7     Static intA;8     Static intb = 1;9 Ten      PublicC () { OneE = 3; A     } -  -     { thec = 2; -D = 2; -E = 2; -     } +     intC; -     intD = 1; +     intE = 1; A  at      Public Static voidMain (string[] args) { - System.out.println (C.A);//2 - System.out.println (C.B);//1 -System.out.println (Newc (). c);//2 -System.out.println (NewC (). d);//1 -System.out.println (NewC (). e);//3 in}
2.2. Analysis

The initialization block assignment of member variables (class variables, instance variables) and the determination of the value of the variable when the variable definition exists. The following scenarios are included:

Scenario 1, static variable definition, and static initialization block change the value of the variable when the variable values exist at the same time.

Case 2, the instance variable definition and the instance initialization block change the final value of the variable when the variable is present.

Case 3, or both of the two variables at the same time change the final value of a variable.

If the initialization block is after the variable definition, then the value is the assignment in the initialization block, so here is the case where the initialization block precedes the definition of the variable .

2.3. Conclusion

(Consider an initialization block that assigns a value to a variable before the variable is defined )

1, for static operation and instance operation of the same situation (case 3), according to the order of execution described in the previous section to determine the value;

2, if the definition of the variable is not explicitly initialized, the member variable is initialized by default, and further if there is an initialization block, the value of the variable is the initialization of the value of the block. Such as:

1 classC {2      PublicC () {3     }4 5     {6A = 3;7     }8     Static {9A = 2;Ten     } One     Static intA; A     Static intb; -  -      Public Static voidMain (string[] args) { theSystem.out.println (C.A);//2 -System.out.println (NewC (). a);//3 -System.out.println (C.B);//0 -     } +}

3, if the definition of the display is initialized, that is, the case 1, 2, the same way of processing:

Explicit initialization of variable definitions the implicit definition and initialization block are assigned two procedures , so if you also include the initialization block code, Java will merge them into one processing, so the conclusion is to explicitly initialize and initialize the block which is in the latter value. Such as:

1 classC {2     //The following about static initialization3     Static {4A = 2;5     }6     Static intA = 1;7     Static intb = 1;8     Static {9b = 2;Tenc = 2; One     } A     Static intC; -  -     { theD = 2; -     } -     intD = 1; -     intE = 1; +     { -E = 2; +F = 2; A     } at     intF; -  -      Public Static voidMain (string[] args) { -System.out.println (C.A);//1 -System.out.println (C.B);//2 -System.out.println (NewC (). c);//2 inSystem.out.println (NewC (). D);//1 -System.out.println (NewC (). e);//2 toSystem.out.println (NewC (). f);//2 +     } -}

Java initialization order (static variables, static initialization blocks, instance variables, instance initialization blocks, constructor methods)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.