Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java method table

I learned a lot about how C++ manages its virtual tables in the presence of inheritance (multiple, virtual etc.) and how it lays the objects in memory.

Now, Java only has to worry about single line of inheritance, no instance method hiding etc. so virtual tables should be a bit simpler in this case. I know that Class files act as "gateways" to method area, where type definitions are stored, including method bytecode I presume. The following two questions come to my mind:

  1. Is there any vtable/method table structure in Java in general? How is it stored and linked to Class objects?
  2. How is inheritance/dynamic method call solved? What I mean is:

Having the following classes and instantiations:

class A{ int f(){...} }
class B extends A{ int f(){...} }

A a = new B();
a.f();

f() in B is called. Is A resolving through Class file of B the right method pointer?

Thanks a lot in advance for your comments.

like image 928
Bober02 Avatar asked Apr 19 '12 09:04

Bober02


1 Answers

The call to a.f() is realized in the java bytecode assembly language as:

aload_1 // load the local variable a, here from local address 1
invokevirtual with index into the constant pool for
    method ref:
        class "A"
        nameAndType "f", "()I"

At run-time possibly the vtable then solves the call to B.f(). But as you may see, the class format is quite abstract, and the JVM has all liberty on loading the class for an "efficient" implementation.

like image 171
Joop Eggen Avatar answered Sep 29 '22 03:09

Joop Eggen