Static constructor usage and initialization sequence

Source: Internet
Author: User

Let's take a look at an example:

Using system;

Class
{
Static int X;
Static ()
{
X = B .y + 1;
}
}

Class B
{
Public static int y = a.x + 1;
Static B (){}
 
Static void main ()
{
Console. writeline ("x = {0}, y = {1}", a.x, B .y );
}

}

The execution result is x = 1, y = 2.

This example mainly describes two aspects: static usage and static initialization sequence. After learning about the static initialization sequence and rules, you can easily understand the answer to this question. The following three types of static objects are involved: static members, static methods, and static constructors. The rules are as follows:

Static constructor of a class in a given applicationProgramThe domain is only executed once. The static constructor is triggered when the following events in the application domain occur for the first time:
1) instances of this class are created.
2) any static member is referenced
3) if the class contains the main method of the execution entry, the static constructor of this class is executed before the main method is called.
4) if the class contains any Staic member, the static members are initialized before the static constructor.
5) if the class contains any static method, these static methods are initialized after the static constructor.
6) for Multiple Static members, their initialization will be performed in the text order and will not change because of the call order.

Now let's take a look at the above application. Class B has a main execution entry, so B gets initialization first, and the sequence is static member y-> static constructor. When initializing y, a.x is referenced, and the compiler starts to initialize class A again (note that Class B Initialization is not completed at this time). The sequence is also static member X-> static constructor. In Class A, X is not assigned an initial value during definition (when defining static variables, assign the initial value as much as possible). The compiler will assign the value 0 (INT type) by default ). Then execute the static constructor. Because Class B Initialization is not completed yet, the B .y value is assigned to the default value 0 by the compiler, therefore, after the static constructor of A is executed, the value of X is changed to 1, and B is returned to continue initialization. The value of Y is 2. Finally, execute main to output values a.x and B .y. On the contrary, if the main method in B is removed from a class C, what is the execution result? Based on the above rules, you can easily get the answer.

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.