5.8. Scope and Access

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, look for the closest enclosing curly brackets { } – this is its scope.

Java has 3 levels of scope that correspond to different types of variables:

The image below shows these 3 levels of scope.

Scope Levels

Figure 1: Class, Method, and Block Level Scope

exercise Check Your Understanding

5-8-1: Click on all the variable declarations that are at Class Level Scope.Remember that the instance variables declared at the top of the class have Class Scope.
public class Name {

    private String first;
    public String last;

    public Name(String theFirst, String theLast) {
        String firstName = theFirst;
        first = firstName;
        last = theLast;
     }
}
5-8-2: Click on all the variable declarations that are at Method Level Scope.Remember that the parameter variables and the local variables declared inside a method have Method Level Scope.
public class Name {

    private String first;
    public String last;

    public Name(String theFirst, String theLast) {
        String firstName = theFirst;
        first = firstName;
        last = theLast;
     }
}

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 keep 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.

coding exercise Coding Exercise

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.

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, as seen below. 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.

5.8.1. groupwork Programming Challenge : Debugging

Debug the following program that has scope violations. Then, add comments that label the variable declarations as class, method, or block scope.

5.8.2. Summary

  • Scope is defined as where a variable is accessible or can be used.

  • Local variables can be declared in the body of constructors and methods. These variables may only be used within the constructor or method and cannot be declared to be public or private.

  • 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.

  • Formal parameters and variables declared in a method or constructor can only be used within that method or constructor.

You have attempted of activities on this page
Next Section - 5.9. this Keyword