See below.
toString()
class CheckingAccount
{
. . . . (Now including the toString()
method.)
}
public class CheckingAccountTester
{
public static void main( String[] args )
{
CheckingAccount account1 = new CheckingAccount( "123", "Bob", 100 );
System.out.println( account1.toString() );
CheckingAccount account2 = new CheckingAccount( "007", "James", 45723 );
System.out.println( account2.toString() );
}
}
When println()
or other method needs a String
,
but only an object reference is given, toString()
is automatically used.
So the above could also be written as:
public class CheckingAccountTester { public static void main( String[] args ) { CheckingAccount account1 = new CheckingAccount( "123", "Bob", 100 ); System.out.println( account1 ); CheckingAccount account2 = new CheckingAccount( "007", "James", 45723 ); System.out.println( account2 ); } }