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?
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?
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.
The static variables are provided the memory in the the same memory segment where the code is stored i.e. Class Area. It is independent of the stack or heap segment of memory. It remains accessible during the complete duration of the program.
Static varibales are shared variables for whole the classs, If one object change the value of variable it will be changed for every object of class