I know the question is a bit generic but I need a generic answer already. So the thing I'am trying to understand is how an object's member variable is accessed from the memory point of view, for example, when a method is called on that object?
For example, I've this simple class;
class Hoo {
public int field;
// constructor, setters, getters
...
}
class Foo {
Hoo hoo;
void setHoo() {
hoo = new Hoo(...)
}
}
how runtime would figure out the memory address of hoo even before it's initialised? Same applies also the below code;
class Foo {
Hoo hoo = new Hoo();
void setHooField() {
hoo.field = 5;
}
}
How would the runtime know the field's address here to store a value on it?
IMO, for Java at least, these variable addresses are mapped to an offset in the class file so the exact location (on virtual memory) can be figured out by adding this offset to the passed object reference (into the virtual method). But no sure...
Any ideas?