aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About StackOverflowError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About StackOverflowError" Watch "About StackOverflowError" New topic
Author

About StackOverflowError

Andry Dub
Ranch Hand

Joined: Nov 06, 2007
Posts: 43
Hi! Why my code cause StackOverflowError exception?

public class R extends Q{
String str="rStr";
static Q q=new R();
public static void main(String []str){
System.out.println(q.str);
}
}

class Q{
String str="qStr";
R r=new R();
}


Java is my love
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
this is such a tricky question and it had been discussed somewhere few months ago in this forum, and a very detailed answer by Kelvin Lim can be found.

click me

my own way to explain...

briefly, because from running the println statement in main(), it invokes call the q of type Q in the instance variable; class R inherits class Q, so it inherits variable("r") of type R in class Q.
THEN, variable("r") of type R goes through what class R offers to use, now back to the q of type Q AGAIN.

this runs and runs in a loop, until your pc blows up (not physically, mentally only)


boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About StackOverflowError
 
Similar Threads
can 2 objects of the same class acess private data of each other?
Default values for static variables
var access
Exception in thread "main" java.lang.StackOverflowError
Dan's mock question