Static member execution sequence

Source: Internet
Author: User

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

  1. **************************************** **********
  2. *
    Jing
     
    Status
     
    Structure
     
    Creation
     
    Letter
     
    Quantity
     
    Exercise
     
    XI
  3. *
    (1
    )
    ① ③ ......
    Execution Order
  4. *
    (2
    ) Output result:
     
    Static ()
  5. * Static B ()
  6. * X = 1
    ,
     
    Y = 2
  7. **************************************** ***********/
  8. Using System;
  9. Class
  10. {
  11. Public static int X;
  12. Static ()//

    Return

  13. {
  14. X = B .Y + 1
    ;
  15. Console. WriteLine ("static ()");
  16. }
  17. }
  18. Class B
  19. {
  20. Public static int Y = A.X + 1
    ;//

    A
    Static member,
  21. //
    Go to
    Static constructor ---->
  22. Static B ()//

    If the static field with an initial value is,
  23. //
    When executing the static constructor of this class,
  24. //
    The initial values must be executed in the order of text.
  25. //
    Go to the initial value setting item ---->
  26. {
  27. Console. WriteLine ("static B ()");
  28. }
  29. Static void Main ()//

    Program entry,
  30. //
    If the class contains
     
    Main
    Method,
  31. //
    The static constructor of this class will be called
     
    Main
    Method.
  32. //
    Go to B
    Static constructor ---->
  33. {
  34. Console. WriteLine ("X = {0}, Y = {1}", A.X, B .Y );//

    Output result
  35. Console. ReadLine ();
  36. }
  37. }

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.