This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

StackOverflowError on object creation  RSS feed

 
Igor Soudakevitch
Author
Ranch Hand
Posts: 38
7
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code works:

However, I get StackOverflowError on Line 2 after uncommenting Line 4. By the looks of it, the error gets thrown during the init stage. Could someone please help me out by explaining what's going on here?
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your program is entered at line 3.
It instantiates an object of type Huh at line 4.
Can you see what happens next, when this object is initialized (line 2)?
 
Igor Soudakevitch
Author
Ranch Hand
Posts: 38
7
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankly, no, I don't see it yet. All I see is that the instance initialization method <init>, which was created by javac for the default, no-arg constructor, throws this SOF error. Looks like, this method get called over and over again, and I don't see why
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 2 gets executed every time an instance of class Huh is created. So, when line 4 runs, then line 2 runs, which creates another instance of Huh, which means that line 2 gets executed again for that new instance, which causes line 2 to get run again to create another instance, etc.

You are essentially creating a Russian Matryoshka Doll with an infinite number of nestings. When the nesting is deep enough, you will have exhausted the stack and hence the exception.
 
Igor Soudakevitch
Author
Ranch Hand
Posts: 38
7
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, yes, yes, I got it now! When h1 is being created, h0 is treated as its field, gets created and initialized in its own turn, and here we go again. I do see the light now. Many thanks!
 
Junilu Lacar
Sheriff
Posts: 11129
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor Soudakevitch wrote:Yes, yes, yes, I got it now! When h1 is being created, h0 is treated as its field, gets created and initialized in its own turn, and here we go again. I do see the light now. Many thanks!

Cool

For the benefit of those who are reading this thread and still don't get it (and so that my effort to write it up isn't wasted ),

Here's a step-by-step trace:
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!