Posts

Showing posts from October, 2012

toString() method in Java

The  toString()  method returns a  textual  representation of an object. A basic implementation is already included in  java.lang.Object  and so because all objects inherit from  java.lang.Object it is guaranteed that every object in Java has this method. Overriding the method is always a good idea, especially when it comes to debugging, because debuggers often show objects by the result of the  toString()  method. So use a meaningful implementation but use it for  technical  purposes. ===============================ToStringClass ====================================== public class ToStringClass { public String toString() { return "Hello I am ToStringClass\n"; } public ToStringClass() { System.out.println(this); } } =====================================MainClass==================================== public class MainClass { public static void main(String[] args) { ToStringClass toStringObject = new ToStringClass();