aspose file tools
The moose likes Beginning Java and the fly likes Static variable and Memory Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Static variable and Memory" Watch "Static variable and Memory" New topic
Author

Static variable and Memory

AmitVijay AVKulkarni
Greenhorn

Joined: Apr 26, 2005
Posts: 17
If I have declared static variable i.e. static int variable , is it stored on Heap or Stack?
E.g.

public class TestStr
{
static int i = 0;

public static void main(String args[])
{
i = 1;
System.out.println(i);
}
}
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
Primitives on Stack and Objects on Heap (irrespective of static behaviour)


Thnx
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I beg to differ. The stack is reserved for local variables in methods (both references and primitives, but not objects). Each thread gets its own stack, and stack frames are created and destroyed as methods begin and end. Anything stored in a stack is not accessible to other threads.

Static variables are stored on the heap along with all objects, but I cannot tell you precisely how. My guess is that each Class instance has a JVM-private reference an object holding its set of static fields.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Static variable and Memory
 
Similar Threads
David Smalley
Local variables
Why i can access static variable with Class Instance
the variable in static method
Static Variable Problem