This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19118|Reply: 2

[Source] Summary of the execution order of static, non-static, and constructive methods in a class

[Copy link]
Posted on 4/6/2017 3:12:32 PM | | | |
1 /*

2 * Several principles

3 * 1. Static member variables

4 * 1. Static member variables are class variables, and all objects share the same memory space

5 * 2. The declaration and definition of static member variables is executed only once when the class is loaded for the first time

6 * 3. When loading a class for the first time, first assign initial values to all static member variables according to the type, and then attach right values to those with rvalues

7 * 2. Static initial block

8 * 1. Static initialization blocks are only executed once when the class is loaded for the first time

9          * ······ Multiple static member variables and static initiation fast reference occurrence order are executed sequentially······

10 * 3. Dynamic member variables

11 * 1. The definition of dynamic member variables is executed before the constructor each time an object is instantiated

12 * 4. Dynamic initialization block

13*1. The dynamic initialization block is executed before the constructor each time an object is instantiated

14          * ······ Multiple dynamic member variables and dynamic initialization blocks are executed in the order in which they appear······

15 * Summary: In general, when a class is loaded for the first time, it is executed in the following order, without inheritance involved

16 * 1. Execute static member variable definition and static initialization block in the order of occurrence

17 * 2. Execute dynamic member variable definition and dynamic initialization block in the order of occurrence

18 * 3. Execute the constructor

19 * When instantiating an object again, only steps 2 and 3 can be executed

20          *  

21          * ············ Member variables and definitions and initialization blocks are executed before the constructor·········

22 * 5. When inheritance is involved, it shall be carried out in the following order

23 * 1. Execute the static member variable definition and static initialization block of the parent class, and execute the static member variable definition and static initialization block of the subclass

24 * 2. Execute the non-static member variable definition and dynamic initialization block of the parent class, and execute the parent class construction method

25 * 3. Execute the definition of non-static member variables of subclasses and dynamically initialize blocks, and execute the subclass construction method

26 * Also: If the method used in the parent class construction method has been rewritten by the subclass, then the subclass rewrite method is used when calling the parent class constructor when constructing the subclass object





Previous:.ctor and .cctor of .net
Next:nhibernate native primary key generation policy
 Landlord| Posted on 4/6/2017 3:17:20 PM |


When loading a class for the first time, first assign initial values to all static member variables according to the type, and then execute multiple static member variables in sequence for the attached right value with right values
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com