In-depth discussion of Java class loading mechanism

When is the class initialized:

1) Create an instance of the class, that is, a new object

2) Access a static variable of a class or interface, or assign a value to the static variable

3) Call the static method of the class

4) reflection(Class.forName("com.lyj.load"))

5) Initialize a subclass of a class (the parent class of the subclass will be initialized first)

6) The startup class marked when the JVM starts, that is, the class with the same file name and class name

Only these 6 cases will lead to the initialization of the class of the class. Class initialization steps:

1) If this class has not been loaded and linked, load and link first

2) If this class has a direct parent class, and this class has not been initialized (note: in a class loader, the class can only be initialized once), then initialize the direct parent class (not applicable to interfaces)

  1. If there are initialization statements (such as static variables and static blocks) in the class, execute these initialization statements in sequence.

Quote: https://www.cnblogs.com/xdouby/p/5829423.html

Guess you like

Origin blog.csdn.net/qq_39463175/article/details/130298572