java notes-jvm execution order

The general order of java loading: static block (static variable) -> member variable -> constructor -> static method  .
The execution order is dependent on the class according to the static part, not on the existence of the object, so the loading of the static part takes precedence over the existence of the object.
       1. Static properties, static method declarations, static blocks.
  2. Dynamic properties, common method declarations, building blocks.
  3. Construction method.
static:
  When loading a class, the JVM assigns a default value (generated in one fell swoop) according to the data type of the property. Then initialize static properties and allocate memory space for static properties. The declaration of static methods and the loading of static blocks have no priority. They are executed in the order of appearance , and the static part is only loaded once. At this point, the necessary classes have been loaded and objects can be created.
ordinary:
  When an object is new, the constructor will be called at this time, but before the constructor is called, (the static is completed at this moment, unless it is interrupted and suspended), the dynamic property definition is executed and the default value is set (generated in one fell swoop). Then dynamic properties are initialized, memory allocated, building blocks, common method declarations (just loading, it does not need to be initialized, memory is allocated only when it is called, and the memory is released immediately after the method is executed), there is no priority, in the order of appearance execute . Finally, assign the value in the constructor. When creating an object again, the static part is no longer executed, just the normal part is repeated.
  Note: If there is an inheritance relationship, when an object is created, the dynamic properties will still be defined and default values ​​will be set first, and then the constructor of the parent class will be called . Static initialization may depend on whether the members of the parent class are properly initialized), if the parent class has a parent class, and so on, whether you intend to generate an object of the parent class or not, this happens naturally.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325761314&siteId=291194637