Mamata Gelanee wrote:if objects has two integer variable then it will occupy 4*2 byte..
Stefan Evans wrote:Interpreting the question a little bit: I don't think the question is about memory for the method code itself, but how does memory get allocated when you CALL a method.
There are two main memory stores in java. The "Stack" and the "Heap"
As you have pointed out, memory for Objects created by "new" gets allocated from the heap.
Memory for method calls and the local variables in a method are allocated from the Stack.
When you call a method it allocates memory onto the "top" of the stack for that methods variables.
If you call another method, then more memory gets added to the "top" of the stack.
Every time you call another method, allocates more memory.
You can think of it as "stacking" a pile of boxes up, only ever dealing with the one on top.
When a method returns, the memory it used for local variables can be freed immediately - you can think of this as removing a box from the top of your stack.
The box on top now contains the memory for the method you have just returned to, and so it can keep executing happily.
Of course you can only "stack" the boxes so high before you run out of room. Which is when you encounter a stack overflow error.