- /**
- * Demonstrates how the data inside an
object
- * can be output by overriding
toString ( ) .
- *
- * @author tkrogers
- * @version 10-01-20011
- */
- public class ToStringDemo {
- SomeObject object1 =
new SomeObject( ) ;
-
- public static void
main ( ) {
-
// The tsd instance is being created so that
-
// the instance method, output1 can be called in
-
// main. Otherwise, main can't call an instance method.
- ToStringDemo tsd
= new ToStringDemo ( ) ;
- String y =
"" + 9 ; // 9 is
concatenated to a null
-
// string and becomes a string.
-
System.out.println ( y );
- tsd.output1 ( )
;
- }
-
- public void
output1 ( ) {
- String s =
object1.toString ( ) ;
-
// When an object is used as an argument the toString
-
// method is automatically called for the object. This
-
// converts the object to a sting as defined in the
-
// toString method.
-
System.out.println ( object1 ) ;
-
System.out.println ( s ) ; // Another way to use
toString
- }
- }