Instance variables exist inside objects on the heap. Therefore they are also on the heap. Local variables exist only in the stack. They are not part of the structure of the object. Static variables exist in the classfile structure that is created when the class is loaded. However it is not a simple as that. Stacks hold frames (one frame per method) but the frames may be "heap allocated" so it gets sort of fuzzy. Technically the Method area may be heap allocated also (depending on who wrote the JVM). For all practical purposes though, the word "heap" is used to describe just the area that holds the objects and arrays.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Originally posted by Swath: Hi, I am new to computer field, so what do heap, stack means? Can anyone explain me. Thanks in advance.
Did you try a google search?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
subject: When will a variable use memory from stack and when will use memory from heap?