Variable Scope
Source: Kevin Taylor About.com
Java has three kinds of variables: local (automatic), instance, and class.
Local variables are declared within a method body. Both instance and class
variables are declared inside a class body but outside of any method bodies.
Instance and class variables look similar, except class variables are prepended
with the static modifier.
General Form
Let's take a look at the general form for declaring each kind of variable in a
Java class. The following class is not real Java code; it is only pseudocode:
class someClass {
// Instance Variable
visibility_modifier variable_type instanceVariableName;
// Class Variable
visibility_modifier static variable_type classVariableName;
returnType someMethod() {
// Local Variable
variable_type localVariableName;
}
}
Variable Scope
Each of the three kinds of variables has a different scope. A variable's scope
defines how long that variable lives in the program's memory and when it
expires. Once a variable goes out of scope, its memory space is marked for
garbage collection by the JVM and the variable may no longer be referenced.
Notice that both instanceVariableName and classVariableName are declared within
someClass but outside of any of its methods (only one method exists in this
example). localVariableName, on the other hand, is declared within someMethod.
Local variables must be declared within methods. This includes being declared
within code blocks nested inside of a method--within an if,
while, or for block, for instance. A variable is local to the code block within which
it is declared. Being declared within a method body makes a variable local to
that method. Being declared within an if block makes the variable local to
that if block, for example. This means that the variable cannot be used outside of
the curly braces within which it was declared. In fact, it can only be used
from the line it is declared on until the code block ends.
- Local variables have the most limited scope. They are only valid from the line they are declared on until the closing curly brace of the method or code block within which they are declared.
- Instance variables are in scope as long as their enclosing object is in scope. An instance variable is a part of the object that contains it and cannot live independently of it. Each instance of an object has its own version of instance variables. For example, if an Order class declares that there is an orderDate instance variable, then every Order object will have its own value for orderDate.
- Class variables are in scope from the point the class is loaded into the JVM until the the class is unloaded. Class are loaded into the JVM the first time the class is referenced: often when the first object of that class in instantiated with the new operator. They are the most long-lived kind of variable and come the closest to being a global variable. A class variable's value is shared by all instances of that class. So, even if there are ten Order model objects that contain a class variable named todaysDate, the value that todaysDate contains is shared by all ten objects--there is only one todaysDate.