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)
*/