What wrong in that now??? You are overriding the toString method of the method in your class and then calling your method toString??? What was the problem here??
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Joined: Jun 30, 2009
Nitish Bangera wrote:What wrong in that now??? You are overriding the toString method of the method in your class and then calling your method toString??? What was the problem here??
Yes I override the method in my class.
But I am confused for this below,
when I try to print the value of the object of class A i.e. System.out.println(a);(A a=new A())
this prints string "This is A tostring" which is returned by toSting() method.
System.out.println(a) and System.out.println(a.toString()) are equivalent. They both call toString which returns a string, which println prints. All objects in Java have a toString method because it is part of Object.
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
By overriding the toString(), you define what should be returned when you want to use you object as a string. This string can be anything you want. It may just return value of fields as a string, or anything else..