Site Tools


Smart Card Solution

User Manual

JavaCard API Samples

Java Card Specification

Knowledge Sharing


7.5.57 invokevirtual

Invoke instance method; dispatch based on class






invokevirtual = 139 (0x8b)


…, objectref, [arg1, [arg2 …]] → …


The unsigned indexbyte1 and indexbyte2 are used to construct an index into the constant pool of the current package ( Section 3.5, Frames), where the value of the index is (indexbyte1 , an instance initialization method, or , a class or interface initialization method. Finally, if the resolved method is protected, and it is a member of a superclass of the current class, and the method is not declared in the same package as the current class, then the class of objectref must be either the current class or a subclass of the current class.

The resolved method reference includes an unsigned index into the method table of the resolved class and an unsigned byte nargs that must not be zero.

The objectref must be of type reference. The index is an unsigned byte that is used as an index into the method table of the class of the type of objectref. If the objectref is an array type, then the method table of class Object ( Section, Classes) is used. The table entry at that index includes a direct reference to the method's code and modifier information.

The objectref 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 those of the selected instance method.

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 arguments are made the values of its first nargs words of 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 Card 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.

Runtime Exception

If objectref is null, the invokevirtual instruction throws a NullPointerException.

In some circumstances, the invokevirtual instruction may throw a SecurityException if the current context ( Section 3.4, Contexts) is not the context ( Section 3.4, Contexts) of the object referenced by objectref. The exact circumstances when the exception will be thrown are specified in Chapter 6 of the Runtime Environment Specification, Java Card Platform, Version 2.2.2. If the current context is not the object's context and the Java Card RE permits invocation of the method, the invokevirtual instruction will cause a context switch ( Section 3.4, Contexts) to the object's context before invoking the method, and will cause a return context switch to the previous context when the invoked method returns.

javacard/jcvm/7.5.57_invokevirtual.txt · Last modified: 2017/05/13 04:09 (external edit)