Vladimir Misic: Scope and Lifetime1 Scope And Lifetime Material Borrowed from Nan Schaller.

Slides:



Advertisements
Similar presentations
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Advertisements

Road Map Introduction to object oriented programming. Classes
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
OOPDA Review. Terminology class-A template defining state and behavior class-A template defining state and behavior object-An instance of a class object-An.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Values, variables and types © Allan C. Milne v
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Copyright © Curt Hill Flow of Control A Quick Overview.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Memory Management.
Classes and Objects.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Java Primer 1: Types, Classes and Operators
Using local variable without initialization is an error.
Lecture 11 B Methods and Data Passing
More Object Oriented Programming
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
User Defined Functions
Overloading and Overriding
6 Chapter Functions.
Unit-1 Introduction to Java
CHAPTER 6 GENERAL-PURPOSE METHODS
Compound Statements A Quick Overview
References Revisted (Ch 5)
Corresponds with Chapter 5
String Objects & its Methods
Threads and concurrency / Safety
Presentation transcript:

Vladimir Misic: Scope and Lifetime1 Scope And Lifetime Material Borrowed from Nan Schaller

Vladimir Misic: Scope and Lifetime2 this this is a final variable that holds a reference to the object in which it exists (i.e., this IS a reference to the current object) The type of this is the reference type of the object It is sometimes necessary to pass a reference to the current object as a parameter to another method. this may also be used to refer to another constructor of the same class.

Vladimir Misic: Scope and Lifetime3 Example – this as a reference type Remember that when a class is constructed, the writer of the class has no way to know the name of any objects that will be declared of that class. In some cases, we need a way to refer to the current object. In addition, we might want to use a variable name, such as x and y in our Point class in a couple of different ways, such as instance variable names and in parameter lists … Enter this

Vladimir Misic: Scope and Lifetime4 Example – this as a reference type We were very careful in our Point class to name the parameters of this Point constructor as nextX and nextY. This was because we would otherwise have a problem writing the inside of the constructor, e.g., x = x; y = y; … // Instance Variables private double x; private double y; … public Point( double nextX, double nextY ) { x = nextX; y = nextY; numberOfPoints = numberOfPoints + 1; } … xy x = x; y = y; AARGH!!!

Vladimir Misic: Scope and Lifetime5 Example – this as a reference type Notice that this can help us! It gives a way to refer to the instance variables for the current object. In this case, this is the name of the current object! … // Instance Variables private double x; // X coordinate of the Point private double y; // Y coordinate of the Point … public Point( double x, double y ) { this. x = x; this. y = y; numberOfPoints = numberOfPoints + 1; } … Notice that the identifiers x and y are the nearest declared x and y!

Vladimir Misic: Scope and Lifetime6 Example – this as a constructor We may run into a similar issue when talking about constructors of a class. For example…. –The initial state of an object might involve providing initial values to a large number of variables representing the state of that object. –Sometimes, the only difference in what happens from one constructor to another affects a limited number of those variables. –It would be nice to be able only write the code once, especially as this limits the possibility of error while maintaining that code! Therefore, it would be handy if we could invoke another constructor inside the same class. However, if we invoked it normally, we’d get more storage for the same object! … Enter this

Vladimir Misic: Scope and Lifetime7 Example – this as a constructor In our original Point class we had three constructors which we wrote individually public Point( ) { x = 0.0; y = 0.0; numberOfPoints = numberOfPoints + 1; } public Point( double nextX, double nextY ) { x = nextX; y = nextY; numberOfPoints = numberOfPoints + 1; } public Point( Point otherPoint ) { x = otherPoint.x; y = otherPoint.y; numberOfPoints = numberOfPoints + 1; }

Vladimir Misic: Scope and Lifetime8 Example – this as a constructor Notice what we can do with this – particularly helpful if we have lot’s of stuff to do in the constructor! public Point( ) { this ( 0.0, 0.0 ); } public Point( double nextX, double nextY ) { x = nextX; y = nextY; numberOfPoints = numberOfPoints + 1; } public Point( Point otherPoint ) { this ( otherPoint.x, otherPoint.y ); // or this ( otherPoint.getX(), otherPoint.getY()); } If you use to refer to another constructor, it must be the first statement in the constructor Note this alternative!

Vladimir Misic: Scope and Lifetime9 Scope –A variable’s scope is the region of a program within which the variable can be referred to by its simple name. –Scope determines when the system creates and destroys memory for the variable. –Questions to ask yourself about scope: What variables are known where? When does memory for each variable get allocated? How long does the variable exist? How do we have to name the variable to access it? We’ve already been dealing with some aspects of scope –Talking about writing methods in “library” classes –The use of this –Declaring the counting variable in a for loop –Parameter passing examples

Vladimir Misic: Scope and Lifetime10 public class MyClass { … instance/class variable/constant declarations … public void aMethod ( method parameters ) { local variable declarations … for ( int counter; …) { … } …. } … } Instance/ class variable/ constant Scope Method parmeter Scope Local variable Scope counter variable Scope

Vladimir Misic: Scope and Lifetime11 Scope and Lifetime: Some General Rules The same identifier may be used once within each scope Thus, a class can contain more than one identifier with same name, e.g. –In different methods As parameters As local variables –As instance variables and parameters (=> the need for this !) –As instance variables and local variables (=> the need for this !)

Vladimir Misic: Scope and Lifetime12 Scope and Lifetime: Some General Rules To know which variable is represented when using its simple name –Look within the closest enclosing scope first to see if that variable is declared there. –If not, look at the next closest enclosing scope –Etc. When writing code, you may need to qualify a particular identifier to access it, e.g., –this.x, –otherPoint.x, The name before the point helps us uniquely identify which x we want to use.

Vladimir Misic: Scope and Lifetime13 Scope and Lifetime: Some General Rules The memory for a variable is allocated when it is declared, e.g. –Memory is allocated for method parameters at the time the method is invoked –Local variables in methods are allocated when the method is invoked –Memory for counter variables declared in for loops is allocated when the for loop is executed.

Vladimir Misic: Scope and Lifetime14 Scope and Lifetime: Some General Rules The lifetime of a variable is the duration of execution of the code in the scope it is in, e.g. –Memory is deallocated for method parameters when the execution of a method is complete –Memory is deallocated for local variables in methods when the execution of a method is complete –Memory is deallocated for counter variables declared in for loops when execution of the for loop has completed.

Vladimir Misic: Scope and Lifetime15 Scope Example Very confusing example where a lot of things are named the same – Same example where with no duplicate names –