Skip to content
Advertisement

Where is the super reference in a Java instance method’s stack frame?

I read Bill Venner’s excellent Inside the Java Virtual Machine book, which, in Chapter 5 explores in detail, among other things, the composition of a JVM’s stack frame. (This chapter from the book also happens to be officially published here: https://www.artima.com/insidejvm/ed2/jvm8.html) Apart from this book I studied relatively much the runtime data areas of some JVM’s, especially their stack and heap.

In an instance method’s stack frame, the local variables section constitutes an array of words which holds the method arguments (or parameters), local variables and the “hidden” this reference.

What I’d like to know is, where is the super reference stored, as that is also always available in any non-static context (i.e. instance method body or initializer block), except in the Object class. Is it stored somewhere alongside the reference “this”? If yes, then why is it seemingly always left out from stack frame representations/overviews?

Advertisement

Answer

There is no “super” reference.

When you do:

super.foo()

You “seem” to be calling foo on an object called “super”, but that’s merely Java’s syntax, and doesn’t have to reflect what’s happening under the hood. When this call is translated, it is translated to a invokespecial instruction, that invokes the superclass’s foo method.

Compare this to a this.foo() call, which translates to a invokevirtual instruction. Unlike invokespecial, this will do dynamic dispatch, selecting the right method to call, depending on the runtime type of this.

Note that in both cases, there is a aload_0 instruction before invoking the method, loading the this reference onto the stack.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement