Local/Instance/Class Variables: Java Notes

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Java Notes

Local/Instance/Class Variables
There are three kinds of Java variables:

1. Local variables are declared in a method, constructor, or block. When a method


is entered, an area is pushed onto the call stack. This area contains slots for each
local variable and parameter. When the method is called, the parameter slots are
initialized to the parameter values. When the method exits, this area is popped off
the stack and the memory becomes available for the next called method.
Parameters are essentially local variables which are initialized from the actual
parameters. Local variables are not visible outside the method.
2. Instance variables are declared in a class, but outside a method. They are also
called member or field variables. When an object is allocated in the heap, there is
a slot in it for each instance variable value. Therefore an instance variable is
created when an object is created and destroyed when the object is destroyed.
Visible in all methods and constructors of the defining class, should generally be
declared private, but may be given greater visibility.
3. Class/static variables are declared with the static keyword 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 final and used as either public or private constants.

characteristic Local variable Instance variable Class variable


Where In a method, In a class, but outside a In a class, but outside a
declared constructor, or block. method. Typically method. Must be
private. declared static.
Typically also final.
Use Local variables hold Instance variables hold Class variables are
values used in values that must be mostly used for
computations in a referenced by more constants, variables that
method. than one method (for never change from their
example, components initial value.
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.
Lifetime Created when method Created when instance Created when the
or constructor is of class is created with program starts.
entered. new. Destroyed when the
Destroyed when there program stops.
Destroyed on exit. are no more references
to enclosing object
(made available for
garbage collection).
Scope/Visibility Local variables Instance (field) Same as instance
(including formal variables can been variable, but are often
parameters) are visible seen by all methods in declared public to
only in the method, the class. Which other make constants
constructor, or block in classes can see them is available to users of the
which they are determined by their class.
declared. Access declared access:
modifiers (private,
public, ...) can not be private should be
used with local your default choice in
variables. All local declaring them. No
variables are other class can see
effectively private to private instance
the block in which they variables. This is
are declared. No part regarded as the best
of the program outside choice. Define getter
of the method / block and setter methods if
can see them. A the value has to be
special case is that gotten or set from
local variables outside so that data
declared in the consistency can be
initializer part of a for enforced, and to
statement have a scope preserve internal
of the for statement. representation
flexibility.

Default (also called


package visibility)
allows a variable to be
seen by any class in
the same package.
private is preferable.

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.
Declaration Declare before use Declare anywhere at Declare anywhere at
anywhere in a method class level (before or class level with
or block. after use). static.

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

You might also like