Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Vladimir Misic: Scope and Lifetime1 Scope And Lifetime Material Borrowed from Nan Schaller."— Presentation transcript:

1 Vladimir Misic: vm@cs.rit.edu Scope and Lifetime1 Scope And Lifetime Material Borrowed from Nan Schaller

2 Vladimir Misic: vm@cs.rit.edu 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.

3 Vladimir Misic: vm@cs.rit.edu 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

4 Vladimir Misic: vm@cs.rit.edu 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!!!

5 Vladimir Misic: vm@cs.rit.edu 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!

6 Vladimir Misic: vm@cs.rit.edu 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

7 Vladimir Misic: vm@cs.rit.edu 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; }

8 Vladimir Misic: vm@cs.rit.edu 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!

9 Vladimir Misic: vm@cs.rit.edu 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

10 Vladimir Misic: vm@cs.rit.edu 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

11 Vladimir Misic: vm@cs.rit.edu 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 !)

12 Vladimir Misic: vm@cs.rit.edu 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.

13 Vladimir Misic: vm@cs.rit.edu 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.

14 Vladimir Misic: vm@cs.rit.edu 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.

15 Vladimir Misic: vm@cs.rit.edu Scope and Lifetime15 Scope Example Very confusing example where a lot of things are named the same –http://www.cs.rit.edu/~ncs/Courses/cs1/Scope/http://www.cs.rit.edu/~ncs/Courses/cs1/Scope/ Same example where with no duplicate names –http://www.cs.rit.edu/~ncs/Courses/cs1/Scope2/http://www.cs.rit.edu/~ncs/Courses/cs1/Scope2/


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

Similar presentations


Ads by Google