This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In this case refA is on stack, while object ClassA is on heap. Variable i is on stack, but does its value 10 is on heap or stack ? If its on stack, then it means variable and its value both are on stack for primitive, right ? What I mean to ask is, in case of object, instance reference variable is on stack, but object is on the heap. Likewise, in case of primitive, primitive reference is on stack, but des it value go on stack or heap (which is 10 for i and 20 for j in this case).
I went through various threads, but could not find clear answer to this.
Can someone please answer this without pointing to different thread ?
No, we can't answer that because we don't know whether those are fields or local variables.
Joined: Oct 13, 2005
Now you have changed the code, so we all know i and refA are fields and j and t1 are local variables, where do you think the values associated live, and where the pointers are stored.
Joined: Apr 23, 2009
refA and t1 are reference variables, so they will be on stack.
i and j are primitive variables so they will also be on stack but the their values of 10 and 20 will be on heap.
May be i will also be on heap, but j will be on stack only, but its value 20 eill be on heap.
Only fields (be it primitive or Object references) local to that method live on the Stack. In case of the instance- the memory for this is allocated on the heap and the reference in the stack points to the memory allocated on heap. Each method call will push a StackFrame to the Stack. The Stack frame will have the variables and values for those variables. Values for primitives would be 10,20 and so on and values for references would be the address on the heap for the instance created.
The Instance variables- Those defined in the class/ those part of the instance will be part of the memory allocated for that instance on the heap.
When this code is run, main() is created on stack, reference variable "o" is created on the stack in main(), while instance ObParm is created on heap.
I am confused with the execution of fields and methods of instance created. Can someone let me know, how does theexecution of above code takes place on heap and stack ? I understand it following way, but again I am confused in that.
Where does amethod() get called is it on heap or stack ? amethod has local variable i, which stays on heap and has a reference variable v, which creates another instance on heap. amethod() calls another(). Where does another() get called on heap or stack ?
Please explain step by step execution of this code. I dont understand execution of this code.