Java: <init> 和 <clinit> 有什么区别?

110

我无法理解以下文本...这是不是意味着<clinit>是用于空构造函数的?为什么有两个不同版本很重要?

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html

2.9. Special Methods

At the level of the Java virtual machine, every constructor (§2.12) 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, and they may be invoked only on uninitialized class instances. An instance initialization method takes on the access permissions (§2.7.4) of the constructor from which it was derived.

A class or interface has at most one class or interface initialization method and is initialized (§2.17.4) by invoking that method. The initialization method of a class or interface is static and takes no arguments. It has the special name <clinit>. This name 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 inw2struction, but are invoked only indirectly as part of the class initialization process.

4个回答

170

<init> 是实例的构造函数(之一),用于非静态字段初始化。

<clinit> 是类的静态初始化块,用于静态字段初始化。

class X {

   static Log log = LogFactory.getLog(); // <clinit>

   private int x = 1;   // <init>

   X(){
      // <init>
   }

   static {
      // <clinit>
   }

}

8
CL 代表什么意思? - Ciro Santilli OurBigBook.com
2
@Thilo 这很有趣,因为JVM也将类定义视为另一种对象类型。 - Coder Guy
@JonathanNeufeld 是的,虽然我认为有一些特殊规则。这个方法(由类初始化器调用)被标记为本地... http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/Class.java#352 - cade
1
@Thilo 它也可以代表“ClassLoader”。 - Duncan Calvert

22

14

<init><clinit> 的区别在于,<init> 用于初始化对象实例的构造方法,而 <clinit> 用于初始化类对象本身。例如,当类被加载和初始化时,会在 <clinit> 中完成任何静态类级别字段的初始值设定。


1

补充一点,如果你使用Class.forName方法,它只会初始化类。因此在此方法中,仅调用clinit,并且当你对从forName返回的对象使用newInstance时,它将调用init进行实例初始化。你可以使用下面的代码进行调试。

public class ByteCodeParent
{
 public static String name="ByteCode";
 public ByteCodeParent()
{
    System.out.println("In Constructor");
}

 static
 {
     System.out.println("In Static");
 }

 {
     System.out.println("In Instance");
 }

测试时,请使用

标签


   Class<ByteCodeParent> bcp2 =(Class<ByteCodeParent>) Class.forName("ByteCodeParent");
ByteCodeParent bcp4= bcp2.newInstance();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接