gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:19:38 27 4
gpt4 key购买 nike

想象一个 Java 类,它具有您可以在类中找到的大部分功能。例如:它继承自另一个类,实现了几个接口(interface),包括一些'static final'常量,一些final常量,一些静态变量,实例变量,一个静态 block ,一个未命名的代码块(只是{}中的代码),构造函数、方法等

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

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

最佳答案

这可以在 2.17.4 of the JVMS 5.0/6 部分中描述

2.17.4 Initialization

Initialization of a class consists of:

  • executing its static initializers (§2.11) and
  • the initializers for static fields (§2.9.2) declared in the class.

Initialization of an interface consists of executing the initializers for fields declared in the interface (§2.13.3.1).

Before a class or interface is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized.

A class or interface type T will be initialized immediately before one of the following occurs:

  • T is a class and an instance of T is created.
  • T is a class and a static method of T is invoked.
  • A nonconstant static field of T is used or assigned. A constant field is one that is (explicitly or implicitly) both final and static, and that is initialized with the value of a compile-time constant expression. A reference to such a field must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.

Invocation of certain methods in library classes (§3.12) also causes class or interface initialization. See the Java 2 platform's class library specifications (for example, class Class and package java.lang.reflect) for details.

The intent here is that a type have a set of initializers that put it in a consistent state and that this state be the first state that is observed by other classes. The static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope. This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.

Before a class or interface is initialized its superclass is initialized, if it has not previously been initialized.


Initialization in JVMS 8 is in Chapter 5.5的更新版本

Initialization of a class or interface consists of executing its class or interface initialization method (§2.9).

A class or interface may be initialized only as a result of:

  • The execution of any one of the Java Virtual Machine instructions new, getstatic, putstatic, or invokestatic that references the class or interface (§new, §getstatic, §putstatic, §invokestatic).
    All of these instructions reference a class directly or indirectly through either a field reference or a method reference.
    Upon execution of a new instruction, the referenced class or interface is initialized if it has not been initialized already.
    Upon execution of a getstatic, putstatic, or invokestatic instruction, the class or interface that declared the resolved field or method is initialized if it has not been initialized already.
  • The first invocation of a java.lang.invoke.MethodHandle instance which was the result of resolution of a method handle by the Java Virtual Machine (§5.4.3.5) and which has a kind of 2 (REF_getStatic), 4 (REF_putStatic), 6 (REF_invokeStatic), or 8 (REF_newInvokeSpecial).
  • Invocation of certain reflective methods in the class library (§2.12), for example, in class Class or in package java.lang.reflect.
  • The initialization of one of its subclasses.
  • Its designation as the initial class at Java Virtual Machine start-up (§5.2).

Prior to initialization, a class or interface must be linked, that is, verified, prepared, and optionally resolved.

Because the Java Virtual Machine is multithreaded, initialization of a class or interface requires careful synchronization, since some other thread may be trying to initialize the same class or interface at the same time.
There is also the possibility that initialization of a class or interface may be requested recursively as part of the initialization of that class or interface.

The implementation of the Java Virtual Machine is responsible for taking care of synchronization and recursive initialization by using the following procedure.
It assumes that the Class object has already been verified and prepared, and that the Class object contains state that indicates one of four situations:

  • This Class object is verified and prepared but not initialized.
  • This Class object is being initialized by some particular thread.
  • This Class object is fully initialized and ready for use.
  • This Class object is in an erroneous state, perhaps because initialization was attempted and failed.

关于java - 在 JVM 中加载类时,类的不同部分以什么顺序初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1761377/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com