Bank Account Example public class Bank Account private
Bank Account Example public class Bank. Account { private double balance; public static int total. Accounts = 0; public Bank. Account() { balance = 0; total. Accounts++; } public void deposit( double amount ) { balance += amount; } } public class Driver { public static void main( String[] args ) { Bank. Account a = new Bank. Account(); Bank. Account b = new Bank. Account(); b. deposit( 100 ); } }
// In command prompt java Driver // In Java Virtual Machine Driver. main( args ) Class. Loader loads Driver to the method area args[0] * a main() b Parameters Local variables Frame data 100 A stack frame for main() is pushed into the Java stack Driver class Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 Method Area … Heap Operand stack
// Driver. java public static void main( String[] args ) { Bank. Account a = new Bank. Account(); Bank. Account b = new Bank. Account(); b. deposit( 100 ); } args[0] * a main() 100 * Class. Loader loads Bank. Account() Bank. Account to the method area … b Parameters Local variables A pointer to the Frame data Bank. Account pointer in the heap is Operand stack created (Constant Pool Resolution) Parameters Local variables Frame data Operand stack Parameters Driver class Bank. Account class Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 A stack frame for the Bank. Account constructor is pushed into the Java stack Methods Bank. Account() deposit( double ) Constant Pool 0 Method Area pointer A pointer to the Bank. Account class data is created Heap
// Bank. Account. java private double balance; private static int total. Accounts = 0; args[0] * a main() … b Parameters Local variables Frame data public Bank. Account() { balance = 0; total. Accounts++; } 100 Operand stack Parameters * public void deposit( double amount ) { Bank. Account() balance += amount; } Local variables Frame data Operand stack Parameters Driver class Bank. Account class Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 Methods Bank. Account() deposit( double ) Constant Pool 0 Static Variables total. Accounts 10 Method Area The total. Accounts is incremented static variable byof 1 Bank. Account is initialized with a default value and then assigned with 0 pointer balance 0. 0 Heap The balance variable of this instance is initialized assigned with 0 a default value
// Driver. java public static void main( String[] args ) { Bank. Account a = new Bank. Account(); Bank. Account b = new Bank. Account(); b. deposit( 100 ); } * a Frame data 100 Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 Methods Bank. Account() deposit( double ) Constant Pool 0 Static Variables total. Accounts * Operand stack Local variables Frame data Operand stack The stack frame for the Bank. Account constructor is popped from the Java stack Parameters pointer balance 0. 0 1 Method Area * Parameters Bank. Account() Bank. Account class b main() The pointer is popped from the operand stack and assigned to a Driver class Parameters pointer is returned to the calling. Local framevariables …The args[0] Heap
// Driver. java public static void main( String[] args ) { Bank. Account a = new Bank. Account(); Bank. Account b = new Bank. Account(); b. deposit( 100 ); } Since the Bank. Account class was already loaded in the method area, no other loading happens args[0] * a main() 100 * Bank. Account() … b Parameters Local variables A pointer to the Frame data Bank. Account pointer in the heap is Operand stack created (Constant Pool Resolution) Parameters Local variables Frame data Operand stack Parameters Driver class Bank. Account class Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 A stack frame for the Bank. Account Constructor is pointer pushed into the Java stack Methods Bank. Account() deposit( double ) Constant Pool 0 Static Variables total. Accounts pointer balance 0. 0 1 Method Area Heap A pointer to the Bank. Account class data is created
// Bank. Account. java private double balance; private static int total. Accounts = 0; args[0] * a Parameters … b Local variables main() Frame data public Bank. Account() { balance = 0; total. Accounts++; } Operand stack 100 Nothing happens since the total. Accounts was public void deposit( double amountalready ) { initialized Bank. Account() when the balance += amount; Bank. Account class } was first loaded Parameters Local variables * Frame data Operand stack Parameters Driver class Bank. Account class Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 Methods Bank. Account() deposit( double ) Constant Pool 0 Static Variables total. Accounts The balance variable of this instance is initialized assigned with 0 a default value total. Accounts is incremented by 1 pointer balance 0. 0 21 Method Area Heap pointer balance 0. 0
// Driver. java public static void main( String[] args ) { Bank. Account a = new Bank. Account(); Bank. Account b = new Bank. Account(); b. deposit( 100 ); } args[0] * Parameters … a b Local variables main() Frame data 100 The pointer is popped from the operand stack and assigned to b Operand stack * Parameters * Bank. Account() The pointer is Local variables returned to the calling frame Frame data Operand stack Driver class Bank. Account class Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 Methods Bank. Account() deposit( double ) Constant Pool 0 Static Variables total. Accounts The stack frame for the Bank. Account Constructor is popped from the Java stack pointer balance 0. 0 2 Method Area Parameters Heap 0. 0
// Driver. java public static void main( String[] args ) { Bank. Account a = new Bank. Account(); Bank. Account b = new Bank. Account(); b. deposit( 100 ); } args[0] * a Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 Static Variables total. Accounts Operand stack amount=100 Parameters The object reference to the instance is always put as the first local variable of a stack frame Local variables 100 is popped from the operand Framestack data and put into the next frame’s Operand parameters stack Parameters pointer balance 0. 0 2 Method Area Local variables 100 Methods Bank. Account() deposit( double ) Constant Pool 0 Parameters Frame data deposit( double ) Bank. Account class b main() b Driver class … Heap A stack frame for the deposit method of instance ‘b’ is pointer pushed into the Java balance stack 0. 0
// Bank. Account. java private double balance; private static int total. Accounts = 0; args[0] * a Parameters … b Local variables main() Frame data public Bank. Account() { balance = 0; total. Accounts++; } Operand stack amount=100 Parameters b Local variables public void deposit( double amount ) { Bank. Account() balance += amount; } Frame data Operand stack Parameters Driver class Bank. Account class Methods main() Constant Pool “Bank. Account” a “Bank. Account” b 100 Methods Bank. Account() deposit( double ) Constant Pool 0 Static Variables total. Accounts pointer balance 0. 0 2 Method Area 100. 0 Heap The frame knows which balance to modify because of the object reference
- Slides: 10