Example with Static Variable

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Programming Methodology (1). Implementing Methods main.
Phil Campbell London South Bank University Java 1 First Steps.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
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.
CSCI 160 Midterm Review Rasanjalee DM.
Constructors And Instantiation. Constructor Basics Every class must have a constructor Even abstract classes!! No return types Their names must exactly.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
SEEM3460 Tutorial Java Programming in Unix. Code Translation Java source code Java bytecode Java compiler Bytecode interpreter machine code for target.
תרגול 12 מעקב אובייקטים 1. Our exams material : Course Syllabus : includes all the material.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Case study Students. Array of objects Arrays can hold objects (ref to objects!) Each cell in an array of objects is null by default Sample: from student.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
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.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Classes - Intermediate
Inheritance, Polymorphism and Abstract Classes. Student Management System All students are CUNY Students CUNY Students are Queens College students, or.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Staples are our staple Building upon our solution.
Java Programming in Unix
Java Memory Management
Java Memory Management
9.1 Class (static) Variables and Methods
Examples of Classes & Objects
using System; namespace Demo01 { class Program
Interface.
Implementing Classes Yonglei Tao.
Section 5.7 Concurrency, Interference, and Synchronization.
CompSci 230 Software Construction
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Functions Used to write code only once Can use parameters.
March 29th Odds & Ends CS 239.
CSC 113 Tutorial QUIZ I.
Java: Getting Started Basic Class Structure
CNT 4007C Project 2 Good morning, everyone. In this class, we will have a brief look at the project 2. Project 2 is basically the same with project 1.
Classes & Objects: Examples
Topic 4 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“
Implementing Classes Chapter 3.
Assignment 7 User Defined Classes Part 2
Singleton design pattern
Code Animation Examples
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)
class PrintOnetoTen { public static void main(String args[]) {
Java Programming with Multiple Classes
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Scope of variables class scopeofvars {
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

Example with Static Variable Start class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 mvY Y.value 12 a 14

Example with Static Variable - 1 Initial class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y Comments: Before the program starts, but after it’s loaded into memory, the class hierarchy is loaded and the static variables are instantiated as part of the class load.

Example with Static Variable - 2 One class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 Comments: The line just executed accomplished the following – all the code shown in red is involved: Reserved memory for the mvZ handle Reserved memory for a Z instance Reserved memory for a Y instance Inited X.data to 5 in this instance Inited Y.value to 7 for this Y instance Returned handle to new Y instance, setting value Z.a Same for Z and mvZ Y.value

Example with Static Variable Two class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 Comments: The line just executed accomplished the following – all the code shown in red is involved As before for mvY mvY Y.value 12 a 14

Example with Static Variable Three class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 Comments: The line just executed accomplished the following – all the code shown in red is involved This line of code uses toString from Z, which then calls getValue in Y mvY Y.value 12 a 14

Example with Static Variable Four class X { static int count = 0; private int data; public X(int n) { data = n; } // end int constructor public int getData() { return data; } // end getData } // end class X public class Z extends X { private Y a = new Y(3); public Z(int x, int y) { super(x); a.setValue(y); } // end int, int constructor public String toString () { return "From Z: " + a.getValue(); } // end toString public static void main(String[] args) { Z myZ = new Z (5, 7); Z myY = new Z (12, 14); System.out.println("value myZ: " + myZ); System.out.println("value myY: " + myY.a); } // end main } // end class Z Class Hierarchy – in memory Object X count Z Y Memory Map – instance vars class Y { private int value; public Y(int n) { value = n; } // end int constructor public void setValue(int z) { value = z; } // end method getValue public int getValue() { return value; } // end method getValue public String toString () { return "From Y: " + value; } // end toString } // end class Y mvZ X.data 5 a 7 Comments: The line just executed accomplished the following – all the code shown in red is involved Here the call is to the toString method in Y mvY Y.value 12 a 14