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() 方法反序列化。
 
 

Java: What is the difference between <init> and <clinit>?的更多相关文章

  1. JVM思考-init和clinit区别

    JVM思考-init和clinit区别 目录:JVM总括:目录 clinit和init的区别其实也就是Class对象初始化对象初始化的区别,详情看我上一篇博客: JVM总括四-类加载过程.双亲委派模型 ...

  2. 深入理解jvm--Java中init和clinit区别完全解析(转)

    转自:http://blog.csdn.net/u013309870/article/details/72975536 init和clinit区别 ①init和clinit方法执行时机不同 init是 ...

  3. <init>与<clinit>,static与final与static final

    <init>和<clinit> init是对象构造器方法,初始化对象的时候执行 clinit是类构造器方法,类加载的初始化阶段执行 final常量赋值(必须是一下其中一种) 显 ...

  4. Java Virtual Machine (JVM), Difference JDK, JRE & JVM – Core Java

    By Chaitanya Singh | Filed Under: Learn Java Java is a high level programming language. A program wr ...

  5. <init>与<clinit>的区别

    在编译生成class文件时,会自动产生两个方法,一个是类的初始化方法<clinit>, 另一个是实例的初始化方法<init> <clinit>:在jvm第一次加载c ...

  6. Java <clinit> & <init>

    在编译生成class文件时,会自动产生两个方法,一个是类的初始化方法<clinit>, 另一个是实例的初始化方法<init>.     <clinit>:在jvm第 ...

  7. Invocation of init method failed; nested exception is java.text.ParseException: '?' can only be specfied for Day-of-Month or Day-of-Week.

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cronTrigger' ...

  8. java执行顺序之深入理解clinit和init

    原文地址:https://blog.csdn.net/qq_36522306/article/details/80582758 前言: 最近研究了深入理解JVM这本书中的知识,对java中各部分执行的 ...

  9. 200个最常见的JAVA面试问题(附答案)

    本文内容: 20个最常见的JAVA面试问题(附答案) 13个单例模式JAVA面试问题(附答案) 说说JVM和垃圾收集是如何工作的(附答案) 说说如何避免JAVA线程死锁(附答案) Java中HashS ...

随机推荐

  1. Adroid真机调试

    几次想学Android,都因为启动模拟器调试时太慢而放弃. 今天终于搞通了真机调试,记录一下: 1)USB线把手机和电脑连接. 2)Adroid手机启用USB调试. 3)命令行运行 adb devic ...

  2. Web项目java.lang.OutOfMemoryError: PermGen space异常解决

    接手一个新的Web项目,编译运行(Tomcat版本为7),运行的时候报出了java.lang.OutOfMemoryError: PermGen space的异常,搜了一下这样解释:   PermGe ...

  3. php执行shell不阻塞方法

    大家都知道php执行系统命令的方法有: system() 输出并返回最后一行shell结果. exec() 不输出结果,返回最后一行shell结果,所有结果可以保存到一个返回的数组里面. passth ...

  4. HTTP与HTTPS有什么区别?

    HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全,为了保证这些隐私数据能加密传输,于是网景公司设计了SSL(Secure Sockets Layer)协议用 ...

  5. 1028: [JSOI2007]麻将

    1028: [JSOI2007]麻将 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2638  Solved: 1168[Submit][Status] ...

  6. mysql基础(5)-关联(mysql+pandas)

    表关联类型 内连接: 仅显示满足条件的行 From T1,T2 where T1.ID=T2.ID From T1 inner join T2 ON T1.ID=T2.ID 左连接: 显示左表T1中的 ...

  7. python爬虫-异常处理

    URLerror产生原因: 网络未连接(即不能上网) 服务器不存在 #-*-coding:utf--*- import urllib2 request=urllib2.Request('http:// ...

  8. SQL索引工作原理

    SQL 当一个新表被创建之时,系统将在磁盘中分配一段以8K为单位的连续空间,当字段的值从内存写入磁盘时,就在这一既定空间随机保存,当一个8K用完的时候, SQLS指针会自动分配一个8K的空间.这里,每 ...

  9. 185. Department Top Three Salaries

    问题描述 解决方案 select b.name Department,a.name Employee,a.salary Salary from Employee a,Department b wher ...

  10. 【51Nod】-1326 遥远的旅途

    Description 一个国家有 N 个城市, 这些城市被标为 0,1,2,...N-1. 这些城市间连有 M 条道路, 每条 道路连接两个不同的城市, 且道路都是双向的. 一个小鹿喜欢在城市间沿着 ...