Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on StackOverflow error

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
Please find the below program and let me know why it throws StackOverFlowError.

class Program
{
Program program1 = new Program();
public static void main(String[] args)
{
Program program2 = new Program();
}
}

Thanks and Regards,
Pradeep
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This class has a member variable program1 that points to a Program object. Every instance of Program will have its own "program1" and it's own Program to refer to . But the Program that program1 points to will have its own program1 variable, with its own Program object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object. And that Program will have its own variable with its own object....


And so when you try to create a single Program in main(), the program goes into an infinite recursion, calling the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, which calls the Program constructor, .... which eventually overflows the stack.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here, the constructor in class program is invoked, then the instance variable program1 is initialized, which again is a call to the constructor.Then the actual constructor runs, but then why does it in a stack overflow error???
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
Here, the constructor in class program is invoked, then the instance variable program1 is initialized, which again is a call to the constructor.Then the actual constructor runs, but then why does it in a stack overflow error???


Read my response more carefully. Remember that the object pointed to by "program1" also has its own "program1" variable, which points to another; and that object has another, and another, and another.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are effectively saying "each time you make a Program object, it has to hold a program object, so create another one".

Similar to me giving you the following directions:

1) EVERY time you say "hello", you must then say the word "hello".

2) Say the word "Hello".

if you follow these directions literally, you will say "hello" forever. the JVM tries to create the first object, but it has to hold a program object, which has to hold a program object, which has to... and you eventually overflow the stack.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic