Explain the loading order of classes in Java

This article introduces the loading sequence of classes in Java. Let's see the detailed introduction below:

1. When the virtual machine loads Java classes for the first time, it initializes static initialization blocks, static member variables and static methods

2. An instance of the class is created only when the new method is called

3. Class instance creation process: initialize according to the parent-child inheritance relationship, first execute the initialization block of the parent class, and then the construction method of the parent class; Then execute the initialization block of the subclass inherited by this class, and finally the construction method of the subclass

4. When a class instance is destroyed, the child class part is destroyed first, and then the parent class part is destroyed

Example

output

The static method in the class is loaded on the first call, and the static members in the class are loaded in the order in which they appear in the class. Output when static method 2 is called

Comment out parent parentStaticMethod();

Remove the comment child = new child();

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>