/** SimpleBankAccount.java - illustrate instance and local cariables
**/
public class SimpleBankAccount {
// Instance variables
private String id; // these are all "instance" variables
private String name; // of associated person
private String address; // to send statements to
private long balance;
public SimpleBankAccount(String id, String name, String address) {
// "this" refers to "this object" or "this instance of the class"
this.id = id;
this.name = name;
this.address = address;
balance = 0; // same as this.balance = 0;
}
public String getName() {
return name;
}
public void addToBalance (long amount) {
balance = balance + amount;
}
public void calculateInterest () {
double interest; // local variable
interest = balance * interestRate;
balance = balance + (long) interest; // "casting"
}
public float getBalance() {
return balance;
}
private static double interestRate = .06; // class variable
static void setInterestRate(double newRate) {
interestRate = newRate;
}
}
|