Local/Instance/Class Variables: Java Notes
Local/Instance/Class Variables: Java Notes
Local/Instance/Class Variables
There are three kinds of Java variables:
Initial value None. Must be Zero for numbers, false Same as instance
assigned a value before for booleans, or null variable, and it addition
the first use. for object references. can be assigned value
May be assigned value in special static
at declaration or in initializer block.
constructor.
Access from Impossible. Local Instance variables Class variables are
outside variable names are should be declared qualified with the class
known only within the private to promote name (eg,
method. information hiding, so Color.BLUE). They
should not be accessed can also be qualified
from outside a class. with an object, but this
However, in the few is a deceptive style.
cases where there are
accessed from outside
the class, they must be
qualified by an object
(eg, myPoint.x).
Name syntax Standard rules. Standard rules, but are static public final
often prefixed to variables (constants)
clarify difference from are all uppercase,
local variables, eg with otherwise normal
my, m, or m_ (for naming conventions.
member) as in Alternatively prefix the
myLength, or this as variable with "c_" (for
in this.length. class) or something
similar.
Copyleft 2004 Fred Swartz MIT License