Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jvm

I am unable to understand the following text... Does it mean that <clinit> is for empty constructors? Why is important to have two different versions?

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.

like image 231
Jayan Avatar asked Dec 15 '11 08:12

Jayan


People also ask

What in init () method do in Java?

Init method is a predefined method to initialize an object after its creation. Init method is a life cycle method for servlets for java. It is started by the browser when java program is loaded and run by the browser. Init method is a predefine method to initialize an object after its creation.

What is the work of JVM?

The role of JVM in Java JVM is specifically responsible for converting bytecode to machine-specific code and is necessary in both JDK and JRE. It is also platform-dependent and performs many functions, including memory management and security.


2 Answers

<init> is the (or one of the) constructor(s) for the instance, and non-static field initialization.

<clinit> are the static initialization blocks for the class, and static field initialization.

class X {     static Log log = LogFactory.getLog(); // <clinit>     private int x = 1;   // <init>     X(){       // <init>    }     static {       // <clinit>    }  } 
like image 195
Thilo Avatar answered Sep 28 '22 08:09

Thilo


<init> denotes a constructor, <clinit> denotes a static initializer: "Static Initialization Blocks" in the Java Tutorial, Static initializer in Java.

like image 21
Eli Acherkan Avatar answered Sep 28 '22 10:09

Eli Acherkan