A static variable is allocated for the entire duration of a program's execution, so neither stack nor heap are convenient for it. Then where is that variable? Shouldn't there be some place where it is loaded from?
2) Static variables are allocated memory in data segment, not stack segment.
After the java 8 version, static variables are stored in the heap memory.
Static variables are stored in RAM, just like your global variables.
Variables are usually stored in RAM. This is either on the heap (e.g. all global variables will usually go there) or on the stack (all variables declared within a method/function usually go there). Stack and Heap are both RAM, just different locations.
We have 3 segments in our memory:
Stack Segment — contains local variables and Reference variables (variables that hold the address of an object in the heap).
Heap Segment — contains all created objects in runtime, objects only plus their object attributes (instance variables).
Code Segment — the segment where the actual compiled Java bytecodes resides when loaded. Static members (variables or methods) are called class members, meaning they reside where the class (bytecode) resides, which is in the Code Segment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With