/********************************************************************** * * Name : BankDemo.java * Book Title : The Java(TM) Programming Language, 3rd Edition * ISBN : 0-201-70433-1 * Author : Ken Arnold, James Gosling, David Holmes * Publisher : Addison Wesley (www.awl.com) * Page(s) : 124-125 (read Chapter 5 for detailed explanations) * Copyright : 2000 by Sun Microsystems, Inc. * ***********************************************************************/ /* In this example, a class 'BankAccount' is created that lets you see the last action performed on the account, such as a deposit or a withdrawal. The concept of inner classes is used in this example. */ class BankAccount { // all member variables are private ! private long accNumber; // account number private long balance; // current balance private Action lastAct; // last action performed // Now we implement the inner class Action: its objects represent // what action has been performed and what amount has been involved. public class Action { private String act; private long amount; Action(String act, long amount) { this.act = act; this.amount = amount; } public String toString() { // identify our enclosing account return accNumber + ": " + act + " " + amount; /* Note that private "accNumber" field of the enclosing BankAccount object is used directly! An inner class can access all members of its enclosing class because the inner class is part of the enclosing class. We can also use the full name of the reference to the enclosing object: BankAccount.this and access "accNumber" as BankAccount.this.accNumber */ } } public void deposit(long amount) { balance += amount; lastAct = new Action("deposit", amount); // an inner Action object is created and by default is // associated with an object of its enclosing class } public void withdraw(long amount) { balance -= amount; lastAct = this.new Action("withdraw", amount); // an inner Action object is created and // associated with an object of its enclosing class } public void transfer(BankAccount other, long amount){ other.withdraw(amount); // "other" refers to BankAccount object from which we take money deposit(amount); // a default "this" refers to the current BankAccount object /* an inner Action object is created and associated with the current enclosing BankAccount object */ lastAct = this.new Action("transfer", amount); /* an inner Action object is created and associated with the other BankAccount object */ other.lastAct = other.new Action("transfer", amount); } public Action getLastAction() { return lastAct; } public BankAccount(long number, long balance) { this.accNumber = number; this.balance = balance; lastAct = null; } } public class BankDemo { public static void main(String[] args) { BankAccount acc1 = new BankAccount(12345, 100); BankAccount acc2 = new BankAccount(67890, 2000); acc1.deposit(1000); System.out.println("Last Action = " + acc1.getLastAction()); acc2.withdraw(500); System.out.println("Last Action = " + acc2.getLastAction()); acc1.transfer(acc2, 500); System.out.println("Last Action = " + acc1.getLastAction()); System.out.println("Last Action = " + acc2.getLastAction()); } } /********************************************************************** * The output of this program is the following: * * Last Action = 12345: deposit 1000 * Last Action = 67890: withdraw 500 * Last Action = 12345: transfer 500 * Last Action = 67890: transfer 500 * ***********************************************************************/