Static constructor:
(1
) Is used to initialize static and read-only fields.
(2
) Add static
Keyword. Access modifiers cannot be added because static constructors are private.
(3
The static constructor of a class can be executed at most once in a given application domain: only when an instance of the class is created or any static member of the referenced class is activated.
(4
Static constructor cannot be inherited and cannot be called directly.
(5
) If the class contains the Main
Method, the static constructor of this class will call the Main
Method.
(6
) Any static field with an initial value setting item, the initial values must be executed in the text order before executing the static constructor of this class.
(7
) If no static constructor is compiled, and the class contains static fields with initial values, the compiler automatically generates the default static constructor.
Non-static constructors are also applicable: When calling any static member, you must first execute those initial values in the text order, first static fields, and then general fields.
1. When a class is instantiated, first check whether the static fields in the class have been initialized. If not initialized, the initialization code of the static fields will be executed first. (If the static field is called again in the initialization code
To initialize the static fields in the other class .) The Code of non-static fields is executed, and then the class constructor is executed.
2. When you call a static method or attribute of a class, you must first check whether the static field in the class has been initialized. If no Initialization is performed, the initialization code of the static field will be executed first. Then execute the code inside the static method or attribute.
3. when a static field of a class exists in a function body, no matter where the static field is in the function body, as long as the static field is not initialized, it will take precedence over before the function is executed, initialize the static field (in fact, all the static fields in the class are initialized ).
Java
Code
- **************************************** **********
- *
Jing
Status
Structure
Creation
Letter
Quantity
Exercise
XI
- *
(1
)
① ③ ......
Execution Order
- *
(2
) Output result:
Static ()
- * Static B ()
- * X = 1
,
Y = 2
- **************************************** ***********/
- Using System;
- Class
- {
- Public static int X;
- Static ()//
④
Return
③
- {
- X = B .Y + 1
;
- Console. WriteLine ("static ()");
- }
- }
- Class B
- {
- Public static int Y = A.X + 1
;//
③
A
Static member,
- //
Go to
Static constructor ---->
- Static B ()//
②
If the static field with an initial value is,
- //
When executing the static constructor of this class,
- //
The initial values must be executed in the order of text.
- //
Go to the initial value setting item ---->
- {
- Console. WriteLine ("static B ()");
- }
- Static void Main ()//
①
Program entry,
- //
If the class contains
Main
Method,
- //
The static constructor of this class will be called
Main
Method.
- //
Go to B
Static constructor ---->
- {
- Console. WriteLine ("X = {0}, Y = {1}", A.X, B .Y );//
⑤
Output result
- Console. ReadLine ();
- }
- }