Scope


When you write a method declaration, you can choose almost any argument names you like. Of course, the names have to be legal (beginning with a letter, underscore, or dollar sign, and continuing with the same plus digits). Moreover, the name should be indicative of the argument's meaning. But beyond these considerations, you have complete latitude. In particular, you are allowed to reuse a variable name that has been used elsewhere in your program.

Every Java variable has a scope. A variable's scope is the matching pair of open and closed curly brackets that most tightly encloses the variable's declaration. Another way to say this is in terms of blocks. A block is a contiguous piece of code that begins with an open curly and ends with a matching closed curly. Blocks may contain many kinds of code. We have already seen blocks that contain method bodies. Later in this book, we will see blocks that contain, among other things, other blocks. Those inner blocks can contain, among other things, still other blocks, and so on, to whatever depth is useful.

Already we have seen blocks that contain other blocks, since every Java application is a block that looks like this:

1.  public class ClassName 2.  { 3.    // Optional other methods. 4.    public static void main(String[] args) 5.    { 6.      … 7.    } 8.  } 

Any variable defined in the main method has a scope that spans from line 5 through line 7, since that is the tightest matched pair of curlies that would contain the variable's declaration.

The scope of a method argument is the method itself, even though the argument is actually declared just before the open curly that begins the scope.

Now, here is why it is so important to know about scope: A variable name may not be declared more than once in a single scope. However, a name that is declared in one scope may be declared and used in any number of other scopes. Each declaration refers to a different variable; the variables just happen to have the same name. The situation is similar to filenames in directories. Names must be unique within any particular directory, but a filename that appears in one directory may be used in another directory. The two files have nothing to do with each other, and the common name is just a coincidence.

Consider the following example:

 1. public class ReusesNames  2. {  3.   static void printTriple(int x)  4.   {  5.     int i = 3*x;  6.     System.out.println("Triple = " + i);  7.   }  8.  9.   public static void main(String[] args) 10.   { 11.     int x = 10; 12.     int i = x+5; 13.     printTriple(i); 14.   } 15. }

Here we have two methods, main and printTriple, each with its own scope. Each method's scope has its own i and its own x, unrelated to the i and x of the other method. Each method can use and modify its own i and x, but cannot touch the i and x of the other method.

A convenient effect of Java's scoping rule is that when you write a method, you don't have to worry about whether a variable name you like is already in use in a different method. This is especially convenient in a long program that might have hundreds of methods, each with a dozen variables. If it were not for the scoping rule, we would quickly run out of good variable names.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net