在 JVM 中加载类时,类的不同部分按什么顺序初始化?

2022-01-18 00:00:00 class initialization java classloader

想象一个 Java 类,它具有您可以在一个类中找到的大多数特性.例如:它继承自另一个类,实现了几个接口,包括一些静态最终"常量,一些最终常量,一些静态变量,实例变量,一个静态块,一个未命名的代码块(只是 {} 中的代码),构造函数、方法等.

Imagine a Java class which has most features that you can find in a class. For example: it inherits from another class, implements a couple of interfaces, includes some 'static final' constants, some final constants, some static variables, instance variables, a static block, an unnamed code block (just code in {}), constructors, methods etc.

当有问题的类第一次加载到 JVM 中时,类的各个部分按什么顺序初始化或加载到 JVM 中?JVM 中用于加载的调用堆栈是什么样的?假设这里只有一个类加载器在工作.

When the class in question is loaded into the JVM for the first time, in what order are the various portions of the class initialized or loaded into the JVM? What does the call stack in the JVM look like for the loading? Assume that only one classloader is at work here.

这要回到 Java 的绝对基础/内部原理,但我还没有找到解释正确顺序的好文章.

This is going back to the absolute basics/internals of Java, but I havent been able to find a good article explaining the correct sequence.

推荐答案

JLS,特别是第 12.4 节?

How about the JLS, specifically section 12.4?

相关文章