Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods Scope How are names handled?

Similar presentations


Presentation on theme: "Methods Scope How are names handled?"— Presentation transcript:

1 Methods Scope How are names handled?
Copyright © Curt Hill

2 Recall … A method consists of
A return type A name A parameter list A compound statement Each compound statement creates a new scope block What is scope? Copyright © Curt Hill

3 Duplicate Names Java like the C family may safely use duplicate variable names Not just overloaded method names They must have different scope They may have different types The scoping of variables and how the compiler handles it has some complications We must examine this carefully Copyright © Curt Hill

4 What is Scope? The scope of a variable is those lines where the variable is known Also applies to constants, classes and other named items The scope of a variable starts at the line of its declaration It continues on until the ending of the enclosed compound statement Copyright © Curt Hill

5 Example int sqr(int val){ int temp = val * val; return temp; }
int cube(int x){ int temp = x * x * x; No conflict between the two temp variables since they are in different scope blocks Copyright © Curt Hill

6 Scope The compound statement forms a new scope block
The method parameters are part of this block The Java rule is that any new name may be used The scope of two variables may not overlap The method may access any instance variables or static variables of the class Copyright © Curt Hill

7 Scope Again No code outside the method may look into the method, but the method may access things outside the method Parameters are half way in between Any code that can see the method may see the number and type of the parameters The names are only known inside the method Copyright © Curt Hill

8 Duplicates How does a program handle duplicates?
When a variable name is found Compiler checks current block If found it stops If not it steps into next larger block and checks again When we are done we have found it or complained it is not there Copyright © Curt Hill

9 Sample Functions int y; int fn(int val){ int temp = val * y;
return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © Curt Hill

10 Scope Blocks Drawn int y; int fn(int val){ int temp = val * y;
return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © Curt Hill

11 Look for val int y; int fn(int val){ int temp = val * y; return temp;
} int fn2 (int x){ int temp = x * y + z; Copyright © Curt Hill

12 Look for val int y; int fn(int val){ int temp = val * y; return temp;
} int fn2 (int x){ int temp = x * y + z; Copyright © Curt Hill

13 Look for y variable int y; int fn(int val){ int temp = val * y;
return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © Curt Hill

14 Not local, must be global
int y; int fn(int val){ int temp = val * y; return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © Curt Hill

15 Look for z int y; int fn(int val){ int temp = val * y; return temp; }
int fn2 (int x){ int temp = x * y + z; Copyright © Curt Hill

16 Not found – error int y; int fn(int val){ int temp = val * y;
return temp; } int fn2 (int x){ int temp = x * y + z; Copyright © Curt Hill

17 Duplicate names Since a name is confined to a compound statement duplicate names are acceptable in some cases Cannot define the name twice in same scope Now the most important thing for naming a variable is not whether it has been used before, but a meaningful name Copyright © Curt Hill


Download ppt "Methods Scope How are names handled?"

Similar presentations


Ads by Google