Java: What is the difference between <init> and <clinit>?

Stack Overflow 上的一个问题:Java: What is the difference between <init> and <clinit>?

 
JVM Specification 8, 2.9. Special Methods 是这样描述这两个方法的:
At the level of the Java Virtual Machine, every constructor written in the Java programming language (JLS §8.8) appears as an instance initialization method that has the special name <init>. This name is supplied by a compiler. Because the name <init> is not a valid identifier, it cannot be used directly in a program written in the Java programming language. Instance initialization methods may be invoked only within the Java Virtual Machine by the invokespecial instruction (§invokespecial), and they may be invoked only on uninitialized class instances. An instance initialization method takes on the access permissions (JLS §6.6) of the constructor from which it was derived.

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

Other methods named <clinit> in a class file are of no consequence. They are not class or interface initialization methods. They cannot be invoked by any Java Virtual Machine instruction and are never invoked by the Java Virtual Machine itself.

In a class file whose version number is 51.0 or above, the method must additionally have its ACC_STATIC flag (§4.6) set in order to be the class or interface initialization method.

This requirement was introduced in Java SE 7. In a class file whose version number is 50.0 or below, a method named <clinit> that is void and takes no arguments is considered the class or interface initialization method regardless of the setting of its ACC_STATIC flag.

The name <clinit> is supplied by a compiler. Because the name <clinit> 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.

 
在编译生成 class file 时,JVM 会自动生成两个方法,一个是类的初始化方法 <clinit>, 另一个是实例的初始化方法 <init>。
<clinit>:在 JVM 第一次加载class文件时调用,包括静态变量初始化语句和静态块的执行;
<init>:在实例创建出来的时候调用,包括调用new操作符;调用 Class 或 java.lang.reflect.Constructor 对象的 newInstance() 方法;调用任何现有对象的 clone() 方法;通过 java.io.ObjectInputStream 类的 getObject() 方法反序列化。
 
 
posted @ 2017-12-29 11:28  赫尔修斯  阅读(247)  评论(0编辑  收藏  举报