Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'this' reference stored on call stack in Java?

We already knew that when we calling a method in Java, parameters and local variables will be stored on the stack.

For example the following code:

public class Test
{
    int x = 10;
    int y = 20;

    void test(int y)
    {
        int z = y;
        this.x = y; // How JVM knows where is our current object?
    }

    public static void main(String [] args)
    {
        Test obj = new Test();
        obj.test(3);
    }
} 

Will produce a call stack like the following when we called obj.test():

|             |
+-------------+
|     z       |
|     y       |  obj.test()
+-------------+
|    obj      |  main()
+-------------+

But I'm wondering where does this reference in the method stored? Is it also stored on the stack like the following:

|             |
+-------------+
|   this      |
|     z       |
|     y       |  obj.test()
+-------------+
|    obj      |  main()
+-------------+

Or it is stored on other area in memory? Or is it calculate by the JVM at runtime?

Finally, I'm also curious about does the order of parameters / variable of obj.test() in the stack has a specific order, just like C has calling convention, or it is depend on the implementation of VM?

Update:

I know this is a keyword instead of a normal reference variable, but my primary concern about this example, is how JVM knows where does object laid in the heap?

Or in other words, how can JVM knows what is the current object of a member method at runtime, so they can get access to those instance variable?

like image 310
Brian Hsu Avatar asked Apr 20 '12 05:04

Brian Hsu


People also ask

What is stored on a call stack Java?

The call stack represents all currently active routines--routines that have been called but have not yet returned to their respective caller. A stack frame is a section to the call stack allocated for use by a single function.

Where are reference variables stored in Java?

All objects in Java are stored on the heap. The "variables" that hold references to them can be on the stack or they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also. The Class objects that define Classes are also heap objects.

Where is reference variables allocated?

The reference is basically on the stack. The memory for the object is allocated in what passes for the heap.

Is Call by Reference possible in Java?

There is only call by value in java, not call by reference. If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method.


2 Answers

Most languages that are built around a stackmachine model will work exactly like you described. This includes Java, .NET and C++.

Think about it this way: The code for instance methods is most likely shared across all instances of a class, it wouldn't make much sense to copy anything more than the data for each instance, if that common part (the code for methiod implementations, remember its all just memory to the computer) is the same across all instances anyway.

So what differentiates instance methods from static (in Java and .NET speak) methods is an implicit this parameter that is added to each method signature. The implicit this parameters denotes the instance that the method should operate on. And since parameter passing to method most likely happens over the stack, yes the this parameter will be stored on the stack. (see http://zeroturnaround.com/articles/java-bytecode-fundamentals-using-objects-and-calling-methods/#objects for Java, it's pretty mich the same thing in .NET). The this parameter is pushed as the first parameter onto the stack before calling the method, followed by all other parameters.

Now, that describes the model of the Virtual Machine. If the JITed machine code will really pass the this parameter over the stack or in a register (or in any other way) is entirely implementation specific and transparent to the VM.

One additional thing to beware of in your example code is that you used the variable name 'y' twice, thus in the method the local variable 'y' will shadow the instance variable, except if you explcitily qualify it with 'this'.

like image 160
Johannes Rudolph Avatar answered Sep 28 '22 15:09

Johannes Rudolph


Your Question Is:- how can JVM knows what is the current object of a member method at runtime, so they can get access to those instance variable..

What i know is , when u call a method with its object then implicitly
your object reference is passed to your method. like....

obj.test(obj,3);

And at run time this object is cached in this keyword..  that means this is local
for that method and must be get m/m in stack.
like image 23
Sumit Singh Avatar answered Sep 28 '22 16:09

Sumit Singh