permaculture playing cards*
The moose likes Beginning Java and the fly likes Stack & heap for Static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stack & heap for Static methods" Watch "Stack & heap for Static methods" New topic
Author

Stack & heap for Static methods

Isuru Sampath
Ranch Hand

Joined: Jun 26, 2003
Posts: 57
Can someone kindly explain how does the stack and heap work for static methods?
[ May 25, 2006: Message edited by: Isuru Sampath ]

No Winds No Waves
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
In exactly the same way they work for a non-static method. What's your point of confusion? Can you post some sample code to illustrate?


There is no emoticon for what I am feeling!
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
What does methods have to do with stack and heap?

Object instances are stored in the heap, while their references are stored in the stack. That's all I can say.
Chris Wox
Ranch Hand

Joined: May 25, 2006
Posts: 34
aren't static variables stored in the stack memory ? whears instance variables obviously in the heap ?

may be that was the context of the question ...
Isuru Sampath
Ranch Hand

Joined: Jun 26, 2003
Posts: 57
My apologies for the poor forming of the question. I think Chris understood me right. Thanks chris.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
Originally posted by Chris Wox:
aren't static variables stored in the stack memory ? whears instance variables obviously in the heap ?


No. As Jeff said earlier, declaring something static has no impact on where it is stored.

Objects are always on the heap. Primitives and object references can be on the stack or heap depending on where they are declared.
Chris Wox
Ranch Hand

Joined: May 25, 2006
Posts: 34
Originally posted by Scott Johnson:


Primitives and object references can be on the stack or heap depending on where they are declared.


So local variables always on stack and instance/class variables always on heap ?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Consider what a drawing of this code would look like:

x = new X();
y = x;

Both variables x and y refer to the same object, so there are three "things" in memory: the object and two variables refering to it:

[var x] ----> [object memory] <--- [var y]

So where in memory are these three things? Objects are always allocated on the heap. What about variables x and y? I avoided declaring them, on purpose. If they are local variables, they are allocated in the method's stack frame -- on the stack. Otherwise, they must be fields: either a static field (class variable) or a non-static field (instance variable). In either case, the memory for the reference is declared on the heap.

End of story? Well, a smart allocator could use what's called escape analysis to determine that an object's lifetime is so short and contained that it could be allocated on the stack. They main thing about this, is that if this happened, your code would never notice the difference -- that's semantics for yah. In these "tree falls in the forest" scenarios, does it make a sound? The answer is: you don't care. As long as the semantics of objects and references are what you know them to be, who cares about some stack -vs- heap implementation details? For example, some garbage collectors keep objects' locations fixed in the heap, others may move them around. Should you care? No, although performance characterics of gc may be relevant... This is like a post I saw earlier this week. The poster wanted to know how much memory a reference took up. I asked why and he said he was just curious. Okay, suppose the answer is "4 bytes", and he walks away with the information, then a month later he finds out it is 8 bytes. What's changed in his conception of how things work? Nothing! So aside from knowing that a reference is cheap to allocate and copy, what use is there in knowing the implementation details? It's like asking what elements are used in making the hardware. How important to programming in Java is knowing what percentage of a chip is silicon?

Hmmm... I appear to be ranting...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stack & heap for Static methods
 
Similar Threads
implementation for static and non-static methods stored in memory
Heap and Stack
Static variable and Memory
Memory allocations in java
Where is the Methods place in Memory ?