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 7037159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:30:38+00:00 2026-05-28T01:30:38+00:00

I want to study Java again, because I leave it some years ago. Reading

  • 0

I want to study Java again, because I leave it some years ago. Reading a book I had a problem understanding how Java allocate memory in heap and in stack.

This is what I’ve understood – I’ll try to speak about it with examples.

class TestA {
    int a;

    void methodA(int b) {
        a = b;
    }

    int getA() {
        return a;
    }
}

This is a sample class to show different situation. And this is my main:

int b = 3;

TestA obj = new TestA();
obj.methodA(b);
obj.getA();

So what happen?


## BEGIN

STACK – take some memory for main function

HEAP – empty


## int b = 3

STACK – [take some memory for main function -> here we have b]

HEAP – [empty]


## TestA obj = new TestA()

STACK – [take some memory for main function -> here we have b and a reference to TestA]

HEAP – [take some memory for int a]


## obj.methodA(b);

STACK – [take some memory for main function -> here we have b and a reference to TestA]

HEAP – [take some memory for int a] AND [another memory for methodA]


## execute methodA(int b)

STACK – [take some memory for main function -> here we have b and a reference to TestA] AND [take memory for methodA() -> here we have b used in this function]

HEAP – [take some memory for int a] AND [another memory for methodA]


We have:

  • object AND instance field (primitive or not) in the heap
  • function and scope value in stack

Is it right?

  • 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-05-28T01:30:39+00:00Added an answer on May 28, 2026 at 1:30 am

    At the outset, remember that the heap will also have the Class instance for your class (and several others).

    Re:

    ## TestA obj = new TestA()

    STACK – [take some memory for main function -> here we have b and a reference to TestA]

    HEAP – [take some memory for int a]

    a will be in the heap, not on the stack, as part of the memory allocated for the instance of TestA. b and obj are on the stack, allocated upon entry to main (er, I think that’s when that happens; it could be that the JVM doesn’t reserve stack space for them until it encounters the declarations in the program flow, but there we’re getting into internals of the JVM). The heap also contains the instance of TestA. (Remember that obj, the variable, is quite distinct from what it points to [the instance of TestA]; each of those things takes memory.)

    Also remember that the stack will contain the return addresses for the function calls. E.g., when main calls methodA, the address that the JVM should come back to when methodA returns is also on the stack.

    Various stack structures will also be allocated for exception handling.

    The above is mostly theoretical, mind. JVMs are welcome to do optimizations, and they do (HotSpot is a thoroughly optimizing JVM). @Voo points out, for instance, that JVMs may well put objects on the stack if they detect that they can (for instance, when the object instance is only used within the method and bytecode analysis by the JVM shows that it’s impossible for there to be an outstanding reference to it when the method exits).

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

Sidebar

Related Questions

I have a program where I want to scrap some useful study material for
I want to study some strange functions by plotting them out in mathematica. One
For my study in the university I'm forced to do some ugly java basics,
I want to study OOP-related PHP, mainly for mashups and some development. I started
For short: can you tell me some great resource to study if I want
I'm new at Java, I study on strings and i want a string to
I want to study Cocos2d, seems it's a good framework for iPhone game programming.
I'm completely new in web service. I want to study about it and the
I'm doing a study on large Java projects and would like to view the
I have been working in Java for the past 4 years and I am

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.