public class BankAccount {
public BankAccount () { this(0.0) ; }
public BankAccount( double initBal )
{ balance_=initBal; id_=newID(); }
public double balance() { return balance_; }
public int id() { return id_; }
public void withdraw(double amt) { balance_ -=amt; }
public void deposit(double amt) { balance_ +=amt; }
public String toString()
{ return super.toString() + "(id:" + id_ + ", bal:" + balance_ + ")" ; }
// Instance variables
private double balance_;
private int id_;
// Class variable and class method
public static int nextID_=100;
private static int newID() { return nextID_++; }
// Another "special" class method
public static void main(String args[]) {
BankAccount a=new BankAccount(15.25);
BankAccount b=new BankAccount();
System.out.println("a=" + a.toString() );
System.out.println("b=" + b.toString() );
a.withdraw(5.50);
b.deposit(125.99);
System.out.println("a=" + a);
System.out.println("b=" + b);
System.exit(0);
} // no semi-colon
}
Detailed Description:
"The next major piece of information in this file is necessary to run this as a
Java application. It's a main method. We could have built a separate class that
had the main method in it, so realize that all methods must be in a class,
including the main. The thing to remember is that the application, when run using
the java command, will go and look inside the class and see if it has a main
method in it. If the main method is of the proper signature, meaning it's public, it
returns a void and takes String array. It will match up and call that routine, and
that's the starting point. So, when we ran our application a little while ago, this is
what took place. The system went in, found the main routine, and began to
execute this code. Now, let's walk through it. You've already seen it execute."