+ 1
How does memory management takes place for static and non-static variables ?
My trainer said that static variables get memory under class and non-static variables get memory under object. But I have studied that a class is a logical structure of object and also object is the physical existence of class. If it is a logical structure then how does static variables get memory??
2 Answers
+ 1
When you create an instance of an object/class, a memory block will be allocated in heap for this class and a reference to this object will be created in stack. All local variables (variables that created in methods) lives their short life in stack. Memory management doesn't care if an object is static or non-static. Please google heap and stack.
- 1
a static variables is stored in the static pool. non-static variables are stored in heap. local variables are stored in stack. that is the reason why,we cannot access a local variable globally.