There are three kinds of Java variables:
static
keyword in a class,
but outside a method. There is only one copy. Also called static variables.
Here is a comparison between these three kinds of variables.
characteristic | Local variable | Instance variable | Class variable |
---|---|---|---|
Where declared | In a method, consrtructor, or block. | In a class, but outside a method. Typically private .
|
In a class, but outside a method. Must be declared
static .
|
Use | Local variables hold values temporarily in a method (intermediate calculations, loop variables, ...). | Instance variables hold values that must be referenced by more than one method (for example, components that hold values like text fields, variables that control drawing, etc), or that are essential parts of an object's state that must exist from one method invocation to another. | Class variables are mostly used for constants, variables that never change from their initial value. |
Lifetime | Created when method / constructor / block is entered.
Destroyed on exit. |
Created when instance of class is
created with new.
Destroyed when there are no more references to enclosing object (made avaiable for garbage collection). |
Created when the program starts.
Destroyed when the program stops. |
Scope/Visibility | Local variables (including formal parameters)
are visible only in the method, constructor, or block in
which they are declared.
Access modifiers (private , public , ...)
can not be used with local variables. All local variables
are effectively private. No part of the program outside of the
method/block can see them.
A special case is that local variables declared in the initializer part of a
for statement have a scope of the for statement.
|
Instance (field) variables can been seen by all methods in the
class. Whether they are seen outside the class is
determined by the access they have been declared with:
public Can be seen from any class. private Not visible from any other class. This is regarded as the best choice. Define getter and setter methods if the value has to be gotten or set from outside so that data consistency can be enforced and to preserve internal representation flexibility. protected Not visible from any other class, but visible from any descendant class. This is not a common choice, and some say it should never be used. Default (also called package visibility) Can
be seen by any class in the same package.
Some say |
Same as instance variable, but are frequently declared public when used as named constants. |
Declaration | Declare before use anywhere in a method or block. | Declare anywhere at class level (before or after use). | Declare anywhere at class level with static. |
Initial value | None. Must be assigned a value before the first use. | Zero for numbers, false for booleans, or null for object references. May be assigned value at declaration or in constructor. | Same as instance variable, and it addition can be assigned value in special static initializer block. |
Access from outside | Impossible. Local variable names are known only within the method. | Instance variables should be declared private to promote
information hiding, so should not be accessed from outside a class.
However, in the few cases where there are accessed from outside the
class, they must be qualified by an object (eg, myPoint.x). |
Class variables are qualified with the class name (eg, Color.BLUE). They can also be qualified with an object, but this is a deceptive style. |
Name sytax | Standard rules. | Standard rules or prefixed with m or m_ (for member) as in m_length ,
"_" as in _length , or this as in this.length . |
static public final variables (constants) are all uppercase,
otherwise normal naming conventions. Alternatively prefix the variable
with "c_" (for class) or something similar. |