File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StackOverflowError with 'this' 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 "StackOverflowError with Watch "StackOverflowError with New topic

StackOverflowError with 'this'

amit k prabhat

Joined: Aug 09, 2008
Posts: 6
hi all ,
when try to use 'this' with toString(), I got StackOverflowError..
There is no compile time error but at run time I'm getting error.
Is this happens because 'this'(current obj ref) also try to call toString() implicitly and end with StackOveflowError..then what about next line of error message.. java.lang.AbstractStringBuilder.append(Unknown Source)
UnKnown source why??
please discuss this situation..

class Base{
public String toString(){
return ""+this;

public Base returnBase(){
return this;

public class CheckThis{
public static void main(String [] args){
Base b=new Base();
// or simply call System.out.println(b); or b.toString();

/* run time error
Exception in thread "main" java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at Base.toString(
Quirino Gervacio

Joined: Aug 09, 2008
Posts: 23
Hi Amit! I will try to re-create your error.

SCJP 6.0
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

This error mostly occurred when your program end up in an infite recursive loop and stack assigned for each method call gets exhausted (Hope I used correct word!) .

You are calling Base#toString() methods recursively w/o any exit condition !, See where might be the problem hidden (Hint : look, what happens when you concatenate the string (using + ) with string literal )

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Quirino Gervacio

Joined: Aug 09, 2008
Posts: 23

It seems that in line 5, appending "" to this causes the operation to call toString(), which in case your own toString(), which calls itself.
""+this and ""+this.toString() are the same.

I hope this helps.
[ August 09, 2008: Message edited by: Quirino Gervacio ]
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

Wow! great...The error is indeed in the line ""+this. The toString method is supposed to generate a new string and not depend on the ""+ to generate a string representation for the object..
I would have died in finding the reason for this problem...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Clarence J M Tauro
Ranch Hand

Joined: Aug 09, 2008
Posts: 30
Quirino is perfect.
Any object when concatenated with String, Object's reference is converted to string using the "toString" method in the form of ObjectName@someAddress. So here in your problem, when you are writing as 'return "" + this', here 'this' is called as this.toString().. and which toString(), its your method that you had overridden.

Just remove the toString method from your class, and then call returnBase(), you see that your reference is being printed, why? toString of the super class is called.

Thats it..
amit k prabhat

Joined: Aug 09, 2008
Posts: 6
thanks all java_lovers for their respective views n suggestions...
I agree. Here's the link:
subject: StackOverflowError with 'this'
It's not a secret anymore!