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 ); } }
10
Embed
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Bank Account Examplepublic 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 ); }}