As per "Java 2 Certification Guide" -Simon Roberts etc. page 22, when ever a new object is created, it is stored on the heap however variable containing reference to it will be stored on stack.
made confusion in my head (like who was before a hen or an egg?) An object almost always has reference-variables. Object is on the heap, but his internals are on the stack? I do not quite understand what is the object, where it is and how it gc(-ed). Does it mean that each object creation creates their own heap and stack, per each object? [ January 07, 2003: Message edited by: yidanneuG ninaV ]
(Marilyn fixed link) [ January 07, 2003: Message edited by: Marilyn de Queiroz ]
A local variable is stored on a stack related to the method that contains it. Such a variable only lives as long as the method is going. When the method is over, the stack is gone, and the variable is gone. A local variable may REFERENCE an object (which is on the heap). When the local variable dies, if that is the ONLY reference to the object then the object is available for the gc. The trick is not to confuse the variable with the object that it references. They are two separate things. An object may ALSO contain variables, known as fields (because they are not local to any method). These variables exist until the object is gc'd. Some fun reading: Not all Variables are created Equal How my Dog learned Polymorphism (MUCH more fun than the JVM Specs ) [ January 07, 2003: Message edited by: Cindy Glass ]
"JavaRanch, where the deer and the Certified play" - David O'Meara