The process of java class initialization and object instantiation.

1. When is the class initialized?

  • The class where the main() method is located must be initialized first
  • The first time an object of this class is new, this class will be initialized
  • If the subclass is initialized, the parent class must be initialized first

2. The class initialization process?
Initialization is actually to execute the <clinit> method. A class has only one <clinit> method.
The <clinit> method is the initialization statement of the static member variable and the statement in the static code block.


3. The object instantiation process?
The instantiation of an object actually executes the <init> method.
A class can have multiple <init> methods. An <init> method corresponds to a constructor.
The order of the statements in the <init> method is as follows.

  1. The init of the parent class corresponds to super(), even if it is not written explicitly.
  2. Initialization statements of non-static member variables and statements in non-static code blocks
  3. Statement in the constructor

Guess you like

Origin blog.csdn.net/wx_assa/article/details/108209829