This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StackOverflowError with Watch "StackOverflowError with New topic
Author

StackOverflowError with 'this'

amit k prabhat
Greenhorn

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();
System.out.println(b.returnBase());
// 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(CheckThis.java:3)
*/
Quirino Gervacio
Greenhorn

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: 2902
    
    1

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
Greenhorn

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
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

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
Amit,
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
Greenhorn

Joined: Aug 09, 2008
Posts: 6
thanks all java_lovers for their respective views n suggestions...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StackOverflowError with 'this'