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

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Memory Model of A Program, Methods Overview l Memory Model of JVM »Method Area »Heap »Stack.
Java Programming Review (Part I) Enterprise Systems Programming.
Programming Languages and Paradigms Object-Oriented Programming.
Writing Classes (Chapter 4)
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Class and Object. Class vs Object Let us consider the following scenario… Class defines a set of attributes/fields and a set of methods/services. Object.
Introduction to Object-Oriented Programming
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Programming Languages and Paradigms Activation Records in Java.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Chapter 9 Life & Death of an Object Stacks & Heaps; Constructors Garbage Collector (gc)
Non-Static Classes What is the Object of this lecture?
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Chapter 3 Implementing Classes
RealTimeSystems Lab Jong-Koo, Lim
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Design issues for Object-Oriented Languages
Java Memory Management
Java Memory Management
Examples of Classes & Objects
Implementing Classes Yonglei Tao.
suggested reading: Java Ch. 6
More Object Oriented Programming
CSC 253 Lecture 8.
Chapter Three - Implementing Classes
Java Enter your code from FRQ to Shell
You can work in groups on this program.
CSC 253 Lecture 8.
Stack Memory 2 (also called Call Stack)
Classes & Objects: Examples
د.سناء الصايغ الفصل الأول البرمجة الشيئية
More on Classes and Objects
CS2011 Introduction to Programming I Objects and Classes
Classes and Objects Part 2 Static Class Members and Arrays of Objects
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
JAVA CLASSES.
Introduction to Object-Oriented Programming
Java 1/31/2017 Back to Objects.
Corresponds with Chapter 5
Presentation transcript:

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 ); }

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

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

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 ab

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 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 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 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

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 ab 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 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

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

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 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=