my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B - Statement, Cannot understand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B - Statement, Cannot understand " Watch "K&B - Statement, Cannot understand " New topic
Author

K&B - Statement, Cannot understand

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
"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 .


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
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

Joined: Apr 22, 2006
Posts: 424
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

Joined: Nov 05, 2007
Posts: 661
Thanks.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

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


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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..


Thanks<br />Dinesh
Gaurav Joshi
Greenhorn

Joined: Mar 08, 2008
Posts: 18
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.
 
wood burning stoves
 
subject: K&B - Statement, Cannot understand
 
Similar Threads
Reference Variables on the stack
Heaps & Objects
References variables
Garbage collection
stack and heap variable