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

Vladimir Misic: [email protected] Scope and Lifetime1 Scope And Lifetime Material Borrowed from Nan Schaller

Embed Size (px)

Citation preview

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

Scope and Lifetime1

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

Scope And Lifetime

Material Borrowed from Nan Schaller

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

Scope and Lifetime2

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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.

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

Scope and Lifetime3

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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

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

Scope and Lifetime4

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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;

}…

x y

x = x; y = y; AARGH!

!!

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

Scope and Lifetime5

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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!

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

Scope and Lifetime6

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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

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

Scope and Lifetime7

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

Example – this as a constructor

• In our original Point class we had three constructors which we wrote individuallypublic 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;

}

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

Scope and Lifetime8

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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!

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

Scope and Lifetime9

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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

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

Scope and Lifetime10

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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

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

Scope and Lifetime11

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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

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

Scope and Lifetime12

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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.

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

Scope and Lifetime13

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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.

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

Scope and Lifetime14

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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.

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

Scope and Lifetime15

Vla

dim

ir M

isic

: vm

@cs

.rit

.ed

u

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/