Striking the Balance, Upholding the Seven Principles of Public Life in Regulation
Bank Account Example public class BankAccount { private double balance; public static int...
-
Upload
joseph-peters -
Category
Documents
-
view
233 -
download
0
description
Transcript of Bank Account Example public class BankAccount { private double balance; public static int...
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 ); }}
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
Methodsmain()
// In command promptjava Driver
// In Java Virtual MachineDriver.main( args )
A stack frame for main() is pushed
into the Java stack
main()
Parameters
Local variables
Frame data
Operand stack
*
100
args[0] …
a b
ClassLoader loads Driver to the method
area
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
Methodsmain()
main()
Parameters
Local variables
Frame data
Operand stack
*
100
args[0] …// Driver.javapublic 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 Pool0
MethodsBankAccount()
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)
a b
ClassLoader loads BankAccount to the
method area
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
Methodsmain()
pointer
balance
0.0
BankAccount class
Constant Pool0
MethodsBankAccount()
deposit( double )
Static VariablestotalAccounts
0
main()
BankAccount()
Parameters
Local variables
Frame data
Operand stack
Parameters
Frame data
Parameters
*
*
Operand stack
args[0] …
Local variables
// BankAccount.javaprivate 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
a b
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
Methodsmain()
main()
Parameters
Local variables
Frame data
Operand stack
*
args[0] …// Driver.javapublic 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 Pool0
MethodsBankAccount()
deposit( double )
pointer
*
Static VariablestotalAccounts
1
*
The stack frame for the BankAccount
constructor is popped from the
Java stack
a b
balance
0.0
100
The pointer is returned to the calling frame
The pointer is popped from the operand stack
and assigned to a
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
Methodsmain()
main()
Parameters
Local variables
Frame data
Operand stack
*
args[0] …// Driver.javapublic 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 Pool0
MethodsBankAccount()
deposit( double )
A pointer to the BankAccount class
data is created
pointer
balance
0.0
pointer
*
Static VariablestotalAccounts
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
a b
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
Methodsmain()
balance
0.0
BankAccount class
Constant Pool0
MethodsBankAccount()
deposit( double )
Static VariablestotalAccounts
1
main()
BankAccount()
Parameters
Local variables
Frame data
Operand stack
Parameters
Frame data
Parameters
*
Operand stack
args[0] …
Local variables
// BankAccount.javaprivate 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
a b
The balance variable of this instance is initialized with a
default value
The balance variable of this instance is assigned with 0
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
Methodsmain()
main()
Parameters
Local variables
Frame data
Operand stack
*
args[0] …// Driver.javapublic 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 Pool0
MethodsBankAccount()
deposit( double )
*
Static VariablestotalAccounts
2
*
pointer
balance
0.0
balance
0.0
pointer
100
a b
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
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
BankAccount class
Constant Pool0
Methodsmain() Methods
BankAccount()deposit( double ) pointer
pointer
balance
0.0
balance
0.0
Static VariablestotalAccounts
2
main()
Parameters
Local variables
Frame data
Operand stack
*
args[0] …// Driver.javapublic 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
a b
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
Method Area Heap
Driver class
Constant Pool“BankAccount” a“BankAccount” b
100
BankAccount class
Constant Pool0
Methodsmain() Methods
BankAccount()deposit( double ) pointer
pointer
balance
100.0
balance
0.0
Static VariablestotalAccounts
2
main()
BankAccount()
Parameters
Local variables
Frame data
Operand stack
Parameters
Frame data
Parameters
*
Operand stack
args[0] …// BankAccount.javaprivate 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
a b
b
amount=100
0.0