DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

static method


public class SavingsAccount {
	
	public static void main(String[] args) {
		AccountCaculation myAccount = new AccountCaculation(1000).deposit(2000);
		AccountCaculation yourAccount = new AccountCaculation(2000).deposit(5000);		
		myAccount.printBalance();
		yourAccount.printBalance();
		System.out.println(AccountCaculation.totalBalance);
		System.out.println(AccountCaculation.usd2Rmb(myAccount.balance));
	}

}
class AccountCaculation {

	int balance;	
	static int totalBalance;
	// static method is per class and cannot access object variables
	static int usd2Rmb (int usd) {
		//can access class variables
		System.out.println(totalBalance *6);
		return usd * 6;
	}
	//constructor
	AccountCaculation(int balance) {
		this.balance = balance;
	}
	//classname as type method, return object
	AccountCaculation deposit(int amount) {
		balance += amount;
		totalBalance = totalBalance + balance;
		return this;
	}
	
	void printBalance() {
		System.out.println(balance);		
	}
	
}

Leave a Comment