gpt4 book ai didi

java - 什么调用类初始化方法 以及何时调用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:53:36 24 4
gpt4 key购买 nike

我知道 newdupinvokespecialastore 字节码模式将调用初始化方法 <init>在类实例化期间。什么调用了特殊的 <clinit> 方法,什么时候发生?

我的猜测是 <clinit>之前 <init> 方法被调用。这是否记录在 JVM 规范或 Java 语言规范中?

最佳答案

JVM 规范 §2.9. Special Methods :

A class or interface has at most one class or interface initialization method and is initialized (§5.5) by invoking that method. The initialization method of a class or interface has the special name <clinit>, takes no arguments, and is void (§4.3.3).

The name <clinit> is supplied by a compiler. Because the name is not a valid identifier, it cannot be used directly in a program written in the Java programming language. Class and interface initialization methods are invoked implicitly by the Java Virtual Machine; they are never invoked directly from any Java Virtual Machine instruction, but are invoked only indirectly as part of the class initialization process.

参见 Chapter 5有关类初始化过程的更多详细信息。

关于java - 什么调用类初始化方法 <clinit> 以及何时调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15919317/

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