Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Heaps & Objects

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read Objects are always on Heap,whether its local reference or not.
There is nohing like stack object.All the instance members reside on heap ithin object.
but in K &B book ,chap3 page 271 Q10 says something different.

Some Referene Variables live on stack ,some on heap.
Explanation in book:when an object has a reference variable ,the reference variable lives on stack .

Which is in contradiction to what i read.
Is this true,references inside object ..resides on stack.

THank you
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Lucky..
Consider this code::



here the reference variable 'hs' resides in Heap while the local reference variable 'lhs' resides in
the stack of method heapStackObject(), but the object lhs referring to is
always created on Heap and this Object will have 'hs' as one of it's members(which is on that heap).

now a pop quiz..where do the static reference variable reside??in heap or stack??
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static variable are stored in Static Storage

Static storage. �Static� is used here in the sense of �in a fixed location� (although it�s also in RAM). Static storage contains data that is available for the entire time a program is running. You can use the static keyword to specify that a particular element of an object is static, but Java objects themselves are never placed in static storage

The Final variables are stored in Constant storage area.

Constant Storage:Constant values are often placed directly in the program code, which is safe since they can never change. Sometimes constants are cordoned off by themselves so that they can be optionally placed in read-only memory (ROM).
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More on the first question.
Object references can be placed both on the stack and on the heap depending on where they're declared. If they're declared in a method they will be placed on the stack (during method execution). If they're declared outside a method they will be placed on the heap (and stay there during the lifetime of the object).

The above picture is somewhat changed with version 6 of Java which intriduces an optimization technique called "escape analysis". It allows objects that only lives as long as the method where they're created to be allocated on the stack (insted of on the heap).

So to say that objects are always allocated on the heap will become wrong. Objects with a method-long lifespan can be allocated on the stack.
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic