180 likes | 474 Views
Scope And Lifetime . Material Borrowed from Nan Schaller. 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
E N D
Scope And Lifetime Material Borrowed from Nan Schaller Scope and Lifetime
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. Scope and Lifetime
Example –thisas 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 Scope and Lifetime
Example –thisas a reference type x y • 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; } … • x = x; • y = y; AARGH!!! Scope and Lifetime
Example –thisas 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! Scope and Lifetime
Example –thisas 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 Scope and Lifetime
Example –thisas 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; } Scope and Lifetime
Example –thisas 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 ); // orthis( otherPoint.getX(), otherPoint.getY()); } • If you use to refer to another constructor, it must be the first statement in the constructor Note this alternative! Scope and Lifetime
Scope and Lifetime • 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 Scope and Lifetime
Instance/ class variable/ constant Scope Method parmeter Scope counter variable Scope Local variable Scope public class MyClass { … instance/class variable/constant declarations … public void aMethod ( method parameters ) { local variable declarations … for ( int counter; …) { … } …. } … } Scope and Lifetime
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!) Scope and Lifetime
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. Scope and Lifetime
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. Scope and Lifetime
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. Scope and Lifetime
Scope Example • Very confusing example where a lot of things are named the same • 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/ Scope and Lifetime