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.
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??
"History would be kind to me, for I intend to write it."
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).
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.
SCJP, SCWCD, SCBCD
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo: