3

After searching on the web I did not found yet a good and comprehensive answer about where exactly the instance variables are places inside Java Memory Model. For example we have this code (with shadowing declaration of variables):

class A {
    int var = 1;
    void m() {
        System.out.println("\'m()\' is called from class A");
    }
}

class B extends A {
    int var = 5;
    void m() {
        System.out.println("\'m()\' is called from class B");
    }
}

public class Class1 {
    public static void main(String args[]) {
        A aref = new B();
        aref.m();
        String s = (aref.var)==1?"A":"B";
        System.out.println("\'var\' is called from class " + s);
    }
}

Output of this code is:

'm()' is called from class B
'var' is called from class A

Now the question is not how inheritance works in Java but where in the Java Memory Model this instance variable resides? Please argument your answer.

Thanks

5
  • You have some hiding going on. Commented Nov 14, 2013 at 16:18
  • possible duplicate of Hiding instance variables of a class Commented Nov 14, 2013 at 16:23
  • 1
    "It's in the heap" "No it isn't." "Yes it is!" "No it isn't." "Yes it is!" "No, it isn't." "Look this isn't an argument." "Yes it is." "No it isn't!" "Yes it is." "It's just contradiction!" "No it isn't." "IT IS!" "Ah, you just contradicted me." "No I didn't" "You did!" "When?" "Just now." "Nonsense!" "This is futile!" "No it isn't." Commented Nov 14, 2013 at 16:24
  • In my view is not duplicate because the question IS NOT about inheritance but about JMM. To understand why Java designers made these inheritance rules, we need to understand how JMM works with these data and why it works like that. Commented Nov 15, 2013 at 15:30
  • @ All - I believe the question was not still answered and argument it as requested initially. If there will be others attempting to answer please read the text twice and answer ONLY if you come with a useful and comprehensive answer. Otherwise I believe we will have a lot of words with no added value to all of us. Thanks Commented Nov 19, 2013 at 15:27

2 Answers 2

4

An Object is kept inside the heap, but as one block of memory equal to the size of all variables combined + some extra bytes to store the virtual method table (VMT) and the Object's prototype location (and possibly more depending on the JVM implementation).

So your example Object would look like this in (32-bit) memory (pointer values are just for presentation):

[0000] 0154  // pointer to prototype
[0004] 3625  // pointer to virtual method table
[0008] 0001  // int var

Now in your example above, no member is accessed, so all that the JVM does is to locate the VMT of that prototype, jump to the function address that is written down there and execute it.

If your var = 1 code actually makes it through the optimizer, the resulting assembly code would not know about this "var" thing, and instead work with direct memory access. Something like this:

set [4924 + 8], 1 

Where 4924 is the instance's memory location and + 8 the offset to the variable var. Keep in mind that this is the (made human readable) assembly and not the byte-code, so basically what is left once the JIT is done with it.

Since both your Objects are of the same size, it would even be possible to "upcast" A to B, the only reason it doesn't work, is because Java forbids such unsafe operations. In other less safe languages like C++, you could easily do that and probably get away with it.

Sign up to request clarification or add additional context in comments.

1 Comment

A little bit hard to understand your answer. Please break down the code attached specifying where each piece of code resides on JMM.
1

Variables are different than methods when inheriting. Your method m() is overwritten in your B class when it extends A. However, B cannot override local variables of its parent class, as it does not have any jurisdiction over them.

6 Comments

Another EXCELLENT reason to use getters/setters.
But var is not a local variable here, it is declared outside the method.
In the example above B can access var from A, because the default visibility is protected. So B can do super.var = 3. This however is still bad coding.
@Sentry the variable is local to the class itself. It is not a global variable to everything. You still have to go through the class scope to get to the variable. Therefore, both class A and class B have their own copy of the variables that are local to their class.
@WesleyPorter You're mixing up terms here. Every variable is local to their class or object, because there are no global variables in Java. What you mean are instance variables, non-static fields. Local variables are not of concern here. docs.oracle.com/javase/tutorial/java/nutsandbolts/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.