Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Where reference variables stored ?  RSS feed

 
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
Posts: 20893
81
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!