π5.8: Scope
Table of Contents
π This page is a condensed version of CSAwesome Topic 5.8
Scope & Access of Variables
π The scope of a variable is defined as where a variable is accessible or can be used. The scope is determined by where you declare the variable when you write your programs. When you declare a variable (like int age;
or String name;
), look for the closest enclosing curly braces ({ }
) β this is its scope.
Java has 3 levels of SCOPE that correspond to different types of variables:
-
Class Level Scope for instance variables declared inside a class.
-
Method Level Scope for local variables (including parameter variables) inside a method.
-
Block Level Scope for loop variables and other local variables defined inside of blocks of code with
{ }
.
The image below shows these 3 levels of scope.
Local variables are variables that are declared inside a method, usually at the top of the method. These variables can only be used within the method and do not exist outside of the method. Parameter variables are also considered local variables that only exist for that method. Itβs good practice to declare any variables that are used by just one method as local variables in that method.
Instance variables at class scope are shared by all the methods in the class and can be marked as public
or private
with respect to their access outside of the class. They have Class scope regardless of whether they are public or private.
π Another way to look at scope is that a variableβs scope is where it lives and exists. You cannot use the variable in code outside of its scope. The variable does not exist outside of its scope.
Try the following code to see that you cannot access the variables outside of their scope levels in the toString()
method. Explain to someone sitting next to you why you canβt access these. Try to fix the errors by either using variables that are in scope or moving the variable declarations so that the variables have larger scope.
public class Person {
private String name;
private String email;
public Person(String initName, String initEmail) {
name = initName;
email = initEmail;
}
public String toString() {
for (int i = 0; i < 5; i++) {
int id = i;
}
// Can you access the blockScope variables i or id?
System.out.println("i at the end of the loop is " + i);
System.out.println("The last id is " + id);
// Can toString() access parameter variables in Person()?
return initName + ": " + initEmail;
}
// main method for testing
public static void main(String[] args) {
// call the constructor to create a new person
Person p1 = new Person("Sana", "sana@gmail.com");
System.out.println(p1);
}
}
If there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable. Weβll see in the next lesson, that we can distinguish between the local variable and the instance variable using the keyword this to refer to this objectβs instance variables.
public String toString() {
String name = "unknown";
// The local variable name here will be used,
// not the instance variable name.
return name + ": " + email;
}
π» In-Class Activity: Debugging
βοΈ Summary
-
Scope is defined as where a variable is accessible or can be used.
-
Formal parameters declared in the header/signature of a method or constructor may only be used within the constructor or method and cannot be declared to be
public
orprivate
. Same goes for any local variables declared inside the body of a method or constructor. -
When there is a local variable with the same name as an instance variable, the variable name will refer to the local variable instead of the instance variable. Watch out!
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.