invokespecial

Operation
Invoke instance method; special handling for superclass, private, and instance initialization method invocations

invokespecial
indexbyte1
indexbyte2

Forms
invokespecial = 183 (0xb7)

Stack
..., objectref, [arg1, [arg2 ...]] ...

Description
The unsigned indexbyte1 and indexbyte2 are used to construct an index into the constant pool of the current class (§3.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The item at that index in the constant pool must have the tag CONSTANT_Methodref (§4.4.2), a reference to a class name, a method name, and the method's descriptor (§4.3.3). The named method is resolved (§5.2). The descriptor of the resolved method must be identical to the descriptor of one of the methods of the resolved class.

Next, the Java Virtual Machine determines if all of the following conditions are true:

If so, then the Java Virtual Machine selects the method with the identical descriptor in the closest superclass, possibly selecting the method just resolved.

The resulting method must not be <clinit>, a class or interface initialization method (§3.8).

If the method is <init>, an instance initialization method (§3.8), then the method must only be invoked once on an uninitialized object, and before the first backward branch following the execution of the new instruction that allocated the object.

Finally, if the method is protected (§4.6), then it must be either a member of the current class or a member of a superclass of the current class, and the class of objectref must be either the current class or a subclass of the current class.

The constant pool entry representing the resolved method includes a direct reference to the code for the method, an unsigned byte nargs that must not be zero, and the method's modifier information (see Table 4.4, "Method access and modifier flags").

The objectref must be of type reference and must be followed on the operand stack by nargs - 1 words of arguments, where the number of words of arguments and the type and order of the values they represent must be consistent with the descriptor of the selected instance method.

If the method is synchronized, the monitor associated with objectref is acquired.

If the method is not native, the nargs - 1 words of arguments and objectref are popped from the operand stack. A new stack frame is created for the method being invoked, and objectref and the words of arguments are made the values of its first nargs local variables, with objectref in local variable 0, arg1 in local variable 1, and so on. The new stack frame is then made current, and the Java Virtual Machine pc is set to the opcode of the first instruction of the method to be invoked. Execution continues with the first instruction of the method.

If the method is native and the platform-dependent code that implements it has not yet been loaded and linked into the Java Virtual Machine, that is done. The nargs - 1 words of arguments and objectref are popped from the operand stack; the code that implements the method is invoked in an implementation-dependent manner.

Linking Exceptions
During resolution of the CONSTANT_Methodref constant pool item, any of the exceptions documented in §5.2 can be thrown.

Otherwise, if the specified method exists but is a class (static) method, the invokespecial instruction throws an IncompatibleClassChangeError.

Otherwise, if the specified method is abstract, invokespecial throws an AbstractMethodError.

Otherwise, if the specified method is native and the code that implements the method cannot be loaded or linked, invokespecial throws an UnsatisfiedLinkError.

Runtime Exception
Otherwise, if objectref is null, the invokespecial instruction throws a NullPointerException.

Notes
The difference between the invokespecial and the invokevirtual instructions is that invokevirtual invokes a method based on the class of the object. The invokespecial instruction is used to invoke instance initialization methods (<init>) as well as private methods and methods of a superclass of the current class.

The invokespecial instruction was named invokenonvirtual prior to Sun's JDK 1.0.2 release.