Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where reference variables stored ?

 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anto Telvin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic