new directive simple procedure / class loading simple procedure initialization

Example: Person p = new Person("Zhang San","23");

  1. Because new uses person.class, first find the person.class file and load it into memory (if there is a parent class, load the parent class first)
  2. Execute the initialization of static blocks and static variables (if there is a parent class, initialize the parent class first)
  3. Allocate memory in the heap (if there is a parent class first allocate the parent class, then execute 4,5, then repeat 3,4,5 for the subclass)
  4. Perform initialization of non-static construction blocks and member variables
  5. Execute the constructor code
  6. Assign the memory address to the p variable in the stack memory

Notice:

Static code blocks are directly related to the initialization order of static variables and the order of code before and after

Similarly, the construction of code blocks is directly related to the initialization order of member variables and the order of code before and after

Static is always initialized before non-static

Steps 1 and 2 are the process of class loading

Guess you like

Origin blog.csdn.net/qq_30436011/article/details/129416854