Example of StackOverflowError

One cause of java.lang.StackOverflowError is a method calling itself without exit condition. The following Room class overrides toString method to produce more meaningful string representation, but is implemented wrong:

public class Room {
public static void main(String args[]) {
System.out.println("Created Room: " + new Room());

@Override public String toString() {
return "Room: " + this.toString();
//change to super.toString() to avoid StackOverflowError
Running Room class would fail with java.lang.StackOverflowError.


Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Vũ Diệu Linh said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000