JVM Knowledge

2.9.1. Instance Initialization Methods

A class has zero or more instance initialization methods, each typically corresponding to a constructor written in the Java programming language.

A method is an instance initialization method if all of the following are true:

  • It is defined in a class (not an interface).
  • It has the special name <init>.
  • It is void (§4.3.3).

In a class, any non-void method named <init> is not an instance initialization method. In an interface, any method named <init> is not an instance initialization method. Such methods cannot be invoked by any Java Virtual Machine instruction (§4.4.2, §4.9.2) and are rejected by format checking (§4.6, §4.8).

The declaration and use of an instance initialization method is constrained by the Java Virtual Machine. For the declaration, the method's access_flags item and code array are constrained (§4.6, §4.9.2). For a use, an instance initialization method may be invoked only by the invokespecial instruction on an uninitialized class instance (§4.10.1.9).

Because the name <init> is not a valid identifier in the Java programming language, it cannot be used directly in a program written in the Java programming language.

2.9.2. Class Initialization Methods

A class or interface has at most one class or interface initialization method and is initialized by the Java Virtual Machine invoking that method (§5.5).

A method is a class or interface initialization method if all of the following are true:

  • It has the special name <clinit>.

  • It is void (§4.3.3).

  • In a class file whose version number is 51.0 or above, the method has its ACC_STATIC flag set and takes no arguments (§4.6).

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

Other methods named <clinit> in a class file are not class or interface initialization methods. They are never invoked by the Java Virtual Machine itself, cannot be invoked by any Java Virtual Machine instruction (§4.9.1), and are rejected by format checking (§4.6, §4.8).

Because the name <clinit> is not a valid identifier in the Java programming language, it cannot be used directly in a program written in the Java programming language.

2.9.3. Signature Polymorphic Methods

A method is signature polymorphic if all of the following are true:

  • It is declared in the java.lang.invoke.MethodHandle class or the java.lang.invoke.VarHandle class.
  • It has a single formal parameter of type Object[].
  • It has the ACC_VARARGS and ACC_NATIVE flags set.

The Java Virtual Machine gives special treatment to signature polymorphic methods in the invokevirtual instruction (§invokevirtual), in order to effect invocation of a method handle or to effect access to a variable referenced by an instance of java.lang.invoke.VarHandle.

A method handle is a dynamically strongly typed and directly executable reference to an underlying method, constructor, field, or similar low-level operation (§5.4.3.5), with optional transformations of arguments or return values. An instance of java.lang.invoke.VarHandle is a dynamically strongly typed reference to a variable or family of variables, including static fields, non-static fields, array elements, or components of an off-heap data structure. See the java.lang.invoke package in the Java SE Platform API for more information.