java – 谁调用类初始化器方法和什么时候?

前端之家收集整理的这篇文章主要介绍了java – 谁调用类初始化器方法和什么时候?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道新的,dup,invocation专用和astere字节码模式将调用实例初始化方法< init>当有人从 Java语言的角度来实例化一个 Java类时,但是我从来不知道谁调用了特殊的< 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相关文章