toString methods

I am sorry for posting all this code. I am wondering if someone would be gracious to answer a question regarding the toString() method. Where and how is it called? Is it implicitly called somewhere? Can someone explain this to me please?

/*

* Transactions.java

*

* Created on July 4, 2007, 10:18 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/**

*

* @author pberardi

*/

publicclass Transactions{

publicstaticvoid main(String[] args){

Account acct1 =new Account("Ted Murphy",72354, 102.56);

Account acct2 =new Account("Jane Smith", 69713, 40.00);

Account acct3 =new Account("Edward Demsey", 93757, 759.32);

acct1.deposit(25.85);

double smithBalance = acct2.deposit(500.00);

System.out.println("Smith balance after depost:"+smithBalance);

System.out.println("Smith balance after withdrawal:"+acct2.withdraw(430.75,1.50));

acct1.addInterest();

acct2.addInterest();

acct3.addInterest();

System.out.println();

System.out.println(acct1);

System.out.println(acct2);

System.out.println(acct3);

}

}

/*

* Account.java

*

* Created on July 4, 2007, 10:23 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/**

*

* @author pberardi

*/

import java.text.NumberFormat;

publicclass Account{

privatefinaldouble RATE = 0.035;

privatelong acctNumber;

privatedouble balance;

private String name;

/** Creates a new instance of Account */

public Account(String owner,long account,double initial){

name = owner;

acctNumber = account;

balance = initial;

}

publicdouble deposit(double amount){

balance = balance+amount;

return balance;

}

publicdouble withdraw(double amount,double fee){

balance = balance-amount-fee;

return balance;

}

publicdouble addInterest(){

balance+=(balance * RATE);

return balance;

}

publicdouble getBalance(){

return balance;

}

public String toString(){

NumberFormat fmt = NumberFormat.getCurrencyInstance();

return acctNumber+"\t"+name+"\t"+fmt.format(balance);

}

}

[4606 byte] By [pberardi1a] at [2007-11-27 9:43:14]
# 1

System.out.println(myObject);

out is a static varaible in the System class and it is a PrintStream object. All the print methods are in this class. If you follow the chain of method calls in the API you find it eventually calls the toString method of the object. Since all Classes extend Object either directly or indirectly then all Classes have a toString method if you do not override it yourself.

floundera at 2007-7-12 23:48:13 > top of Java-index,Java Essentials,New To Java...
# 2
muchas gracias Java Nazi flounder
pberardi1a at 2007-7-12 23:48:13 > top of Java-index,Java Essentials,New To Java...
# 3
its good to override the toString method. Specially when playing with arraylist or vectors.
lrngjavaa at 2007-7-12 23:48:13 > top of Java-index,Java Essentials,New To Java...