This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

stackoverflow error

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just want to know how the  above code works..and what to do to get rid of stack overflow error.
 
Ranch Hand
Posts: 40
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Issue appears to be with :

Bird a=new Bird();

 
Luanrkiran kumar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to know how it works.
 
Rancher
Posts: 4450
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's step through it.

When you execute this code, the first thing that the code in main() does is create a new Bird instance.

Now, when a Bird instance is created it has a Bird attribute, which is declared and instantiated at the same time (line 2).
So, main creates a Bird instance, which itself creates a Bird instance...which itself creates a Bird instance...and so on.
 
Ranch Hand
Posts: 94
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Luanrkiran kumar.

Luanrkiran kumar wrote:
I just want to know how the  above code works..and what to do to get rid of stack overflow error.



- a is an isntance of Bird, which is instantiated at construction of Bird object
- When you construct a Bird object a is instantiated calling the Bird constructor another time.
- So every time you call new Bird(), a is created calling new Bird() again.
- So for Bird bird = new Bird(), This code cicles endlessly creating an abject like bird.a.a.a.a.a.a.a.a ....

To escape this code you can change the code in various way.
One option I suggest is to set the variable a static
 
Luanrkiran kumar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, it was helpful.
 
Let's go to the waterfront with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!