Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;

Similar presentations


Presentation on theme: "Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;"— Presentation transcript:

1 Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } } public class Driver { public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); }

2 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() // In command prompt java Driver // In Java Virtual Machine Driver.main( args ) A stack frame for main() is pushed into the Java stack main() Parameters Local variables Frame data Operand stack * 100 args[0]… ab ClassLoader loads Driver to the method area

3 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() Parameters Local variables Frame data Operand stack * 100 args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } A stack frame for the BankAccount constructor is pushed into the Java stack Parameters BankAccount() Parameters Frame data Parameters Operand stack Local variables BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) pointer * A pointer to the BankAccount class data is created A pointer to the BankAccount pointer in the heap is created (Constant Pool Resolution) ab ClassLoader loads BankAccount to the method area

4 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() pointer balance 0.0 BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) Static Variables totalAccounts 0 main() BankAccount() Parameters Local variables Frame data Operand stack Parameters Frame data Parameters * * Operand stack args[0]… Local variables // BankAccount.java private double balance; private static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } The balance variable of this instance is initialized with a default value The totalAccounts static variable of BankAccount is initialized with a default value and then assigned with 0 The balance variable of this instance is assigned with 0 totalAccounts is incremented by 1 1 100 ab

5 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() Parameters Local variables Frame data Operand stack * args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } Parameters BankAccount() Parameters Frame data Parameters Operand stack Local variables BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) pointer * Static Variables totalAccounts 1 * The stack frame for the BankAccount constructor is popped from the Java stack ab balance 0.0 100 The pointer is returned to the calling frame The pointer is popped from the operand stack and assigned to a

6 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() Parameters Local variables Frame data Operand stack * args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } A stack frame for the BankAccount Constructor is pushed into the Java stack Parameters BankAccount() Parameters Frame data Parameters Operand stack Local variables BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) A pointer to the BankAccount class data is created pointer balance 0.0 pointer * Static Variables totalAccounts 1 100 A pointer to the BankAccount pointer in the heap is created (Constant Pool Resolution) Since the BankAccount class was already loaded in the method area, no other loading happens ab

7 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() balance 0.0 BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) Static Variables totalAccounts 1 main() BankAccount() Parameters Local variables Frame data Operand stack Parameters Frame data Parameters * Operand stack args[0]… Local variables // BankAccount.java private double balance; private static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } pointer balance 0.0 pointer * Nothing happens since the totalAccounts was already initialized when the BankAccount class was first loaded totalAccounts is incremented by 1 2 100 ab The balance variable of this instance is initialized with a default value The balance variable of this instance is assigned with 0

8 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 Methods main() Parameters Local variables Frame data Operand stack * args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } Parameters BankAccount() Parameters Frame data Parameters Operand stack Local variables BankAccount class Constant Pool 0 Methods BankAccount() deposit( double ) * Static Variables totalAccounts 2 * pointer balance 0.0 balance 0.0 pointer 100 ab The stack frame for the BankAccount Constructor is popped from the Java stack The pointer is popped from the operand stack and assigned to b The pointer is returned to the calling frame

9 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 BankAccount class Constant Pool 0 Methods main() Methods BankAccount() deposit( double ) pointer balance 0.0 balance 0.0 Static Variables totalAccounts 2 main() Parameters Local variables Frame data Operand stack * args[0]… // Driver.java public static void main( String[] args ) { BankAccount a = new BankAccount(); BankAccount b = new BankAccount(); b.deposit( 100 ); } Parameters deposit( double ) Parameters Frame data Parameters Operand stack Local variables b The object reference to the instance is always put as the first local variable of a stack frame 100 ab b amount=100 A stack frame for the deposit method of instance ‘b’ is pushed into the Java stack 100 is popped from the operand stack and put into the next frame’s parameters

10 Method Area Heap Driver class Constant Pool “BankAccount” a “BankAccount” b 100 BankAccount class Constant Pool 0 Methods main() Methods BankAccount() deposit( double ) pointer balance 100.0 balance 0.0 Static Variables totalAccounts 2 main() BankAccount() Parameters Local variables Frame data Operand stack Parameters Frame data Parameters * Operand stack args[0]… // BankAccount.java private double balance; private static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++; } public void deposit( double amount ) { balance += amount; } Local variables The frame knows which balance to modify because of the object reference ab b amount=100 0.0


Download ppt "Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;"

Similar presentations


Ads by Google