类的整个生命周期从加载到虚拟机内存时开始,到卸载时结束。整个生命周期包括七个阶段:加载、验证、准备、解析、初始化、使用和卸载。核查、准备和解决的三个部分统称为连接。
[En]
The entire life cycle of a class starts when it is loaded into the virtual machine memory and ends when it is unloaded. The whole life cycle includes seven stages: loading, verification, preparation, parsing, initialization, use, and unloading. The three parts of verification, preparation and resolution are collectively referred to as connection.
类初始化情况:
- 遇到new、getstatic、putstatic 或 invokestatic 这4条字节码指令时,如果没有初始化,则需要触发初始化。生成这4条指令的最常见Java代码场景是:使用new关键字实例化对象的时候、读取或设置一个类的静态字段,以及调用一个类的静态方法时。
- 使用java.lang.reflect包的方法对类进行发射调用的时候,如果类没有进行过初始化,则需要先触发其初始化。
- 初始化类时,如果其父类尚未初始化,则需要首先触发其父类的初始化。
[En]
When initializing a class, if its parent class has not been initialized, the initialization of its parent class needs to be triggered first.*
- 当虚拟机启动时,用户需要指定要执行的主类。虚拟机首先初始化这个主类。
[En]
When the virtual machine starts, the user needs to specify a main class to be executed. The virtual machine first initializes this main class.*
以上四个场景成为类的活动引用。此外,所有引用类的方法都不会触发初始化——被动引用。
[En]
The above four scenarios become active references of classes. In addition, all methods of referencing classes will not trigger initialization -- passive reference.
第一种情况
对于静态字段,只初始化直接定义此字段的类。因此,通过父类的子类引用父类中定义的静态字段只会触发父类的初始化,而不会触发子类的初始化。
[En]
For static fields, only the class that directly defines this field will be initialized. Therefore, referencing the static fields defined in the parent class through its subclasses will only trigger the initialization of the parent class, not the initialization of the child class.
```
package main.java.loadclass;
public class SubClass extends SupperClass{
static {

future用法-java中异步执行多个任务并集中处理所有结果

使用 Java Swing 编写 IpChat, 基于 IP Messenger 协议,支持 IPv4/IPv6

智慧树/知到 一键刷课脚本代码(网页自动版 2020最新版)

Java(6)控制流程

类加载器ClassLoader

rabbitmq 一些参数设置

Seata源码分析——RPC模块底层实现

MySQL的三值逻辑

注意:Spring Boot 2.7开始spring.factories不推荐使用了,接下来这么玩…

Dubbo浅入浅出

Storm2

pagination分页插件使用demo

单个 tomcat 下运行多个 war 应用, 问题与解决方法

【Java线程池】 java.util.concurrent.ThreadPoolExecutor 源码分析
