• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

if vars are on stack, but cannot be outside of objs, and objs are always in the heap

 
Ranch Hand
Posts: 898
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I have read

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 ]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic