my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Question on StackOverflow error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on StackOverflow error" Watch "Question on StackOverflow error" New topic
Author

Question on StackOverflow error

Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
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


SCJP 1.6
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Oct 02, 2003
Posts: 11149
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on StackOverflow error
 
Similar Threads
Delete Expired Rows?
persisting variable values
ServerSocket.accept(); problem
Web services, need help on 2 things
WA #1.....word association