aspose file tools*
The moose likes Beginning Java and the fly likes Where reference variables stored ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Where reference variables stored ?" Watch "Where reference variables stored ?" New topic
Author

Where reference variables stored ?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
hi all
if we have a code like this
class chk
{
chk c=new chk();
void disp()
{
......
}
where the reference variable which is declared here as instance will get stored ?

thanks


Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19799
    
  20

On the heap.

Your code is a StackOverFlowError waiting to happen though.

You class "chk" has a field of the same type. You initialize that immediately. Therefore, the following will happen:
- you create chk #1
- this will automatically create chk #2
- this will automatically create chk #3
- this will automatically create chk #4
- this will automatically create chk #5
...

This will continue on forever - until the error is thrown.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thanks
 
Consider Paul's rocket mass heater.
 
subject: Where reference variables stored ?