谁调用类初始化方法< clinit>什么时候? [英] Who invokes the class initializer method <clinit> and when?

查看:118
本文介绍了谁调用类初始化方法< clinit>什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 new dup invokespecial astore 字节码模式将调用实例初始化器方法< init> 当某人从Java语言角度实例化Java类时,但我从不弄清楚谁调用了特殊的< clinit>。 方法以及什么时候发生?

I know that the new, dup, invokespecial and astore bytecode pattern will invoke the instance initializer method <init> when someone instance a Java class from the Java language point of view, but i never figure out who invoke the special <clinit>method and when does this happen?

我的猜测是< clinit> 之前 < init> 方法被调用。任何人都可以给我一些信息来证明这一点吗?这是否记录在JVM规范或Java语言规范中?

My guess is that <clinit> is invoked before <init> method. Can any body give me some information to prove it? Is this documented in the JVM Specification or Java Language Specification?

推荐答案

JVM规范第2.9节。特殊方法


一个类或接口最多具有一个类或接口初始化方法,并通过调用该方法进行初始化(第5.5节)。类或接口的初始化方法具有特殊名称< clinit> ,不带参数,并且是无效的(第4.3.3节)。

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).

名称< clinit> 由编译器提供。因为该名称不是有效的标识符,所以不能直接用Java编程语言编写的程序中使用它。 类和接口初始化方法由Java虚拟机隐式调用。绝对不能从任何Java虚拟机指令直接调用它们,而只能在类初始化过程中间接调用它们。

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.

请参见<有关类初始化过程的更多详细信息,请参见href = http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html rel = noreferrer>第5章

这篇关于谁调用类初始化方法&lt; clinit&gt;什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆