• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B - Statement, Cannot understand

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Some Reference variables live on the Stack while some live on the heap"

This is a statement given in K&B which is true.
Can some one explain that.

I thought objects live on heap and methods,local variables and reference variables live on stack.

This was further explained by
"When and object has a reference variable ,the reference variable lives inside the object on the heap"

I didn't get either of the statements and moreover, it sounded contradictory.
Can some one please explain .
 
Ian Edwards
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you define a class with some instance variables:



when you instantiate a Test object the testBucket instance variable is a reference to an instance of Bucket. The testBucket reference variable lives on the heap as it is a part of the Test object.

In the makeObject() method we are defining a reference variable that will point to a new instance of Bucket - this reference variable will live on the stack as it is a local variable. All local variable live on the stack.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that no matter where the variable is defined i.e instance(on Heap) or local(on Stack) the object itself will always be on the Heap there is No Stack object.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that no matter where the variable is defined i.e instance(on Heap) or local(on Stack) the object itself will always be on the Heap there is No Stack object.


... JVM's are allowed in theory to allocate objects on the stack in theory in certain specific circumstances ...

"Escape analysis

The Java language does not offer any way to explicitly allocate an object on the stack, but this fact doesn't prevent JVMs from still using stack allocation where appropriate. JVMs can use a technique called escape analysis, by which they can tell that certain objects remain confined to a single thread for their entire lifetime, and that lifetime is bounded by the lifetime of a given stack frame. Such objects can be safely allocated on the stack instead of the heap. Even better, for small objects, the JVM can optimize away the allocation entirely and simply hoist the object's fields into registers. "

http://www.ibm.com/developerworks/java/library/j-jtp09275.html
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

i am not able to get these lines
when you instantiate a Test object the testBucket instance variable is a reference to an instance of Bucket. The testBucket reference variable lives on the heap as it is a part of the Test object.

In the makeObject() method we are defining a reference variable that will point to a new instance of Bucket - this reference variable will live on the stack as it is a local variable. All local variable live on the stack.
.

Can you please elaborate little(in easier way) Please..
 
Gaurav Joshi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

According to my understanding.. testBucket reference variable will be stored on stack and actual object will store in heap.
where does i defined in line 1 will store. Stack for sure than
Isn't this the case that testBucket reference variable will store in calling method stack i.e main() or some other thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic