5.6 Scope and Accessibility

 

We have said previously that variables can usually be used at any point after they have been declared. The one exception to this that we have seen is that a variable declared in the initializer part of a f or statement can only be used within the for statement. The range in which an identifier can be recognized is known as its scope. Now that we have programs with more than one method, we must modify our idea of scope. First, we can make our previous ideas of scope within any method (including the main method) more precise. A block is a section of code enclosed by brace brackets. The scope of a variable is the part of the code from the declaration of the variable down to the brace bracket that closes the block in which the variable was declared.

 

Example 1

 

In the method sketched here, the comments indicate the scope of the variables

i, j, and k. (The dots indicate code that we have omitted.)

 

public static void sample ()

{

   int i = 1;            // scope of i starts here

                         // cannot use j or k here

   for (int j = 0; ... ) // scope of j starts here

   {

      int k = 1;         // scope of k starts here

      ...                // can use i, j, and k here

   }                     // scopes of j and k end here

   ...                   // still in scope of i here

}                        //  scope of i ends here

 

 

 

It may be useful to picture each block as a rectangle in which the variables declared in a block are only known inside that block. For the previous example, we might use a diagram like the following in which the scope of an identifier extends from the point at which is declared to the end of the box in which the declaration takes place.

 

 

The scope of a parameter is the entire method in which the parameter is defined, so a parameter behaves like a variable declared at the very start of a method. If a variable is declared inside a block and that block terminates, the identifier can be re-used elsewhere. Java does not, however, allow us to make scopes overlap. Thus, a variable cannot be declared inside a block if it has been previously declared in a containing block.

 

 

Example 2

Consider the following outline of a method:

 

public static int sample (double a)

{

   for (int i = 1; ... )

   {

      ...

   }

   ...

   double i=0;

   ...

}

 

 

We again illustrate the scopes with a diagram.

 

 

Note that, if the double variable i had been declared at the beginning of the method, this would have produced a compilation error because, in that case, the int variable i would be nested inside the scope of the double version.

 

One consequence of the scope rules is that a variable declared in one method cannot be seen in another method. As a result, we do not have to worry about using the same identifiers in different methods. If we make the declaration

       int i = 0;

in one method and the declaration

       int i = 1;

in another method, there is no confusion as each identifier is local to its method.

 

Associated with the concept of scope of variables is that of accessibility of methods. Up to this point, the definitions of all of our methods have started with the word public. The word public is an example of an access control modifier.

 

To start to examine the idea of accessibility, it is necessary to explore (a bit) the way that large projects can be organized in Java. Initially, our programs involved only a main method in a class. Now, in this chapter, we have seen that a class can contain more than one method. Even larger groupings are possible with a number of classes contained in a single package. A package is simply a collection of classes. Java itself is made up of a number of packages such as j ava. applet for classes that can be used to create programs that run inside a web browser and j ava. io for classes that perform input and output. We can specify that a file belongs to a particular package by placing a package statement as the first line of the file. A package statement has the form

 

                              package <package identifier>;

 

If we do not specify that a class is to be placed in a package, it is placed in the default package - the directory in which the program was created.

 

To say that a method is public means that it can be seen and called from anywhere - from any method in any class in any package. The main method must always use this modifier but other methods need not. If we replace the word public by the word private in a method's header, then the method can only be accessed from within the class in which it is defined. A third option is to omit the access control modifier completely. In this case, the method can be accessed from within the package in which it is defined. There are other possibilities but, for now, these three cases are all that we need. We will, shortly, be using multiple classes but we will never be creating more than one package. If all of our classes are contained in one package, public access is equivalent to package access. In these circumstances, we can omit the public modifier for methods other than main.

 

Exercises 5.6

 

l. In the method outline shown here, state which variables can be used at each numbered line.

 

public static void showScope (int i, double x)

{                            // 1

   int j;  

   ...                          // 2

   for (int k 0; .. )

   { 

      ...                       // 3

      double y;                

      ...                       // 4

   }

   ...                          // 5

   {

      float f;

      ...                       // 6

   }

   double z;

   ...                          // 7

}

 

2. What is a block?

 

3.    (a)  What is the difference between defining a method as public and defining it as private?

       (b) What is the result of omitting both public and private from the header of a method?

 

4. For which method is the public modifier always required?