Details are given in the implementation (and not in the specification). However, implementations usually follow a very simple pattern. Most of the memory layout in Java is very simple and straightforward. My terminology may not correspond to the Java terminology, since I do not deal with a lot of Java programming.
In general, an object starts with a pointer to its vtable, and then has a bunch of fields that follow. Fields are either primitive types (int / bool / float) or pointers to objects. What is this for objects. (Classes are also objects.) Null pointers are like C, they are not valid, not like Python, where None is an object.
The inner class has an additional hidden field that points to an instance of the outer class. In this way, inner classes access data in the outer class. Anonymous classes work the same way. Static methods are just class methods, not instance methods.
The vtable has all the magic. Each class has its own virtual table, shared between all objects. Vtable has information about the class, such as the size of its instances and how fields are laid out. This information is used by the garbage collector. The vtable also has pointers to all the methods that the class implements. When you call a method, the runtime first grabs the vtable pointer from the object, then it grabs the method pointer from the vtable, then it calls the method and passes the object to the method as an implicit parameter. It is similar to C ++, but much easier to implement. A process may be skipped if the method or class is "final".
I know that Java really does not have “pointers”, it has “symbolic pens” or something like that, but common implementations just use plain old pointers.
Dietrich epp
source share