0

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?

stdout
  • 2,471
  • 2
  • 31
  • 40
  • Are you specifically asking about how this works in Java? If so, I would include that in the question title or tags. The answer will be very different in say C++, or C#, or Java. – JamesFaix Apr 06 '18 at 09:52
  • Well I was thinking C++ and C# has kinda a similar way (based on the fact that they're converting the source into their corresponding object files - though C++ has no managed environment) but not sure. Anyway, let's say for Java then :). – stdout Apr 06 '18 at 10:17

0 Answers0