• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance Variable initialization gives Stack Overflow

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Following are two versions of the same program but the instance variable is initialized inside a method in one case; and other outside of it. However, when the program is run, only one of them gives a stack overflow (i.e. it runs infinitely) whereas the other does not. Please explain.



Version 1:


Version 2:
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
Your code where you are assigning memory to a variable at the class level will give stack overflow error.
It is because when you assign memory to an object, it allocates memory space to all the instance variables and the functions.
In you case you are creating an object "a" of class X. When you do this, the JVM will start allocating memory to the instance variables.
In your case, "b" is an instance variable to which you are assigning memory at the class level. Since "b" is also an object, the JVM will
again go to assign memory space to the instance variables of class X (which is b in our case). Hence this process will keep on going
till you run out of memory.
So in order to avoid this problem, if you are creating an object of a class as its instance member, avoid assigning memory to it at the class level
or in the constructor.
 
Rajarshi Rakshit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Richa!

However, Java allows such memory assignments at class level (in fact I got one such example in the book 'Head First Java' page #239). So, in case you create (and assign) an object to a different class than X (at the class level), it works!
The problem here is: The constructor called is of the same class as the one in which the code exists in the first place. So there is a kind of recursive call!

However, it is the same with both the Versions (i.e. both are kind of recursive calls). But, only one goes for the stack overflow. Not sure why...
 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajarshi Rakshit wrote:However, it is the same with both the Versions (i.e. both are kind of recursive calls). But, only one goes for the stack overflow. Not sure why...



No, they're not. In the first case, you construct a new object when instantiating the class. That, in turn, instantiates another object of the same class, and so on.
In the second example, another object is only created when you call printing(), and that will not start a recursion.
 
Rajarshi Rakshit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jan... that explains it!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic