There are three kinds of Java variables:
statickeyword in a class, but outside a method. There is only one copy per class, regardless of how many objects are created from it. They are stored in static memory. It is rare to use static variables other than declared
finaland used as either public or private constants.
|characteristic||Local variable||Instance variable||Class variable|
|Where declared||In a method, constructor, or block.||In a class, but outside a method. Typically
||In a class, but outside a method. Must be declared
|Use||Local variables hold values used in computations in a method.||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 or constructor 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 available 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 (
||Instance (field) variables can been seen by all methods in the
class. Which other classes can see them is
determined by their declared access:
private should be your default choice in declaring them. No other class can see private instance variables. 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.
Default (also called package visibility) allows a variable
to be seen by any class in the same package.
public. Can be seen from any class. Generally a bad idea.
protected variables are only visible from any descendant classes. Uncommon, and probably a bad choice.
|Same as instance variable, but are often declared public to make constants available to users of the class.|
|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
||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 syntax||Standard rules.||Standard rules, but are often prefixed to clarify difference from
local variables, eg with