Scope                                                                                     
  
As seen in the code examined so far, variables and methods have a certain range in which they exist and where they
can be accessed. The concept here is known as scope. There are different levels of scope. Scope pertains to where
data exists (to which context it belongs). Scope can most easily be identified by curly brackets { }, in other words,
a code block.

You have seen a number of different locations for variables so far:
       •        Inside of a class
       •        Inside of a class but defined as static
       •        Inside of a method (as parameters to a method or as desc in the method below).


String getDescription(){
 String
desc = "This is a " + this.color + " " + this.type;
 return desc;
}


These are actually different levels of scope, and you refer to them in different ways:
   
    •        Instance variables
    
   •        Static variables (a.k.a. class variables)
    
   •        Local variables

Local variables do not always mean “method” variables. Local variables can refer to variables in any code block,
including for() loops, if statements, and so forth, which you will see in a later chapter. Sometimes variables are
described as “most local” or “local-most,” meaning that it is the nearest variable of a given name, regardless of
scope.  

The following example shows different levels of scope.


public class Car{                //Begin Car scope

String color;                                //Instance variable in Car scope
String type;                                //Instance variable in Car scope
int serialNumber;                        //Instance variable in Car scope
static int carCount;                //Static variable in Car class scope

...

Car(String c, String t){       //Begin constructor scope
color = c;                                //'c' is a local variable
type = t;                                //'t' is a local variable
carCount++;                                
serialNumber = carCount;
}                              //End constructor scope

public String getDescription(){  //Begin getDescription() scope
String desc;                                  //Local variable
desc = “This is a ”+ color + “ “ + type;
return desc;
}                                //End getDescription() scope
}                                  //End Car scope


Notice that the curly braces are a good indicator of scope. Any code that is defined within a given set of curly
braces has access to the enclosing code block’s variables. Notice that the Car() constructor and the getDescription()
method have access to color, type, serialNumber, and even carCount. This is because Car() and getDescription()
are defined within the same code block as those variables (the Car class). There are a few exceptions to this rule,
including static methods accessing nonstatic. There are other exceptions, which will be discussed later (i.e., access
modifiers).

Any variables defined in a given code block only live within that section of code. For example, getDescription()
defines a variable called desc. The desc variable only exists while the method is executing.When the method
finishes, desc no longer exists. When this happens, a variable is said to have “fallen out of scope.”

To help understand scope, it is also important to know that
every method call gets it own stack. However, as
discussed before, all stacks share the same heap. To demonstrate, examine the following BankAccount class.


public class BankAccount{
 double balance = 150.00;                //Instance variable
       
 public void withdraw(double amount){  //amount is a local variable
   balance = balance - amount;
 }
}


Now suppose the following code was run to use BankAccount objects.


   public class ATM{
     public static void main(String[] args){
1:       BankAccount account = new BankAccount();
2:       double amount = 20.00;
3:       account.withdraw(amount);        
4:       System.out.println("Cha-ching!")
;
     }
   }



The diagrams below show how this code translates to memory after lines 1 to 4 are executed. After line 1 is
executed, memory would look like this:















After line 2 is executed, memory would look like this. Remember that primitives are not stored in the heap. Their
values are held in stack space.















After line 3, memory would look like this:























Finally, after line 4 is run, the withdraw method stack is removed from memory.















Notice that each stack has its own “amount” variable within scope. The variables do
not have to be named the
same. The variables could have been named two totally different names. This simply demonstrates that even
though the variables have the same name, they are two different variables in two different scopes.
Scope
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services