Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:09:03+00:00 2026-06-02T13:09:03+00:00

We already knew that when we calling a method in Java, parameters and local

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:09:04+00:00Added an answer on June 2, 2026 at 1:09 pm

    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’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a method that will allow me to take two URL's and
I already audit authorization success, failure and logout. I've considered auditing (logging) every method
I already have code which lazy loads scripts on request. My issue now is
I already have an enterprise Java EE application. I want expose some of the
I have an already ok htaccess script that is designed to forward /about to
So i want to make a code, that creates a binary tree, that holds
i will be working on a project that tries to determine your position using
I have a method (GetDataReader, let's call it) that returns a SqlDataReader. It's inside
I'm new to Objective-C and am currently following a tutorial in a book that
Background A year ago I had to learn PHP in school, I already knew

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.