Java: What is the difference between <init> and <clinit>?
Stack Overflow 上的一个问题:Java: What is the difference between <init> and <clinit>?
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 aclass
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 itsACC_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 itsACC_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.