*
The moose likes Beginning Java and the fly likes stack trace problem, need help understanding. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stack trace problem, need help understanding." Watch "stack trace problem, need help understanding." New topic
Author

stack trace problem, need help understanding.

Carly Davis
Greenhorn

Joined: May 31, 2008
Posts: 9
Why does the code below cause the following error to be repeated when ran:

at Testing.<init><Testing.java:5>

class Testing {
Testing t = new Testing();

public static void main(String [] args) {
Testing t2 = new Testing();
}
}

Thanks so much for your help!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

The problem is infinite recursion, which will probably lead to an out of memory exception.

The way you've written this, every instance of Testing has its own instance of Testing. So when you try to create an instance of Testing, that instance has to have its own instance of Testing, and in turn that instance has to have its own instance, and that instance has to have its own instance, and...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"Pepper Jack," please check your private messages by clicking on My Private Messages. Thanks!
Carly Davis
Greenhorn

Joined: May 31, 2008
Posts: 9
Thanks so much, that makes sense.

Carly, Previously "Pepper Jack"...I was eating pj cheese when I filled out my profile
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Carly Davis:
...I was eating pj cheese when I filled out my profile

Thanks for your prompt attention! I hope you find the ranch helpful!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by marc weber:
The problem is infinite recursion, which will probably lead to an out of memory exception.


Two small corrections:

- it's an OutOfMemory*Error*, and
- in my experience, it will more likely lead to a StackOverflowError.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stack trace problem, need help understanding.
 
Similar Threads
new option() and &amp;
blank page returned from validate
old request when submit !??!
jsp does not display messages
Struts2: I want to use <s:textfield> inside <s:checkbox>, is it possible?