• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Java NotesLocal/Instance/Class Variables
There are three kinds of Java variables:1.
Local variables
are declared in a method, constructor, or block. When a methodis entered, an area is pushed onto the
call stack 
. This area contains slots for eachlocal variable and parameter. When the method is called, the parameter slots areinitialized 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 alsocalled
member 
or 
 field variables
. When an object is allocated in the
heap
, there isa slot in it for each instance variable value. Therefore an instance variable iscreated when an object is created and destroyed when the object is destroyed.Visible in all methods and constructors of the defining class, should generally bedeclared private, but may be given greater visibility.3.
Class/static variables
are declared with the
static
keyword in a class, butoutside a method. There is only one copy per class, regardless of how manyobjects are created from it. They are stored in static memory. It is rare to use staticvariables other than declared
final
and used as either public or private constants.
characteristicLocal variableInstance variableClass variable
Wheredeclared 
In a method,constructor, or block.In a class, but outside amethod. Typically
private
.In a class, but outside amethod. Must bedeclared
static
.Typically also
final
.
Use
Local variables holdvalues used incomputations in amethod.Instance variables holdvalues that must bereferenced by morethan one method (for example, componentsthat hold values liketext fields, variablesthat control drawing,etc), or that areessential parts of anobject's state that mustexist from one methodinvocation to another.Class variables aremostly used for 
constants
, variables thatnever change from their initial value.
 
 Lifetime
Created
when methodor constructor isentered.Destroyed on exit.
Created
when instanceof class is created with
new
.
Destroyed
when thereare no more referencesto 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 visibleonly in the method,constructor, or block inwhich they aredeclared. Accessmodifiers (
private
,
public
, ...) can
not 
beused with localvariables. All localvariables areeffectively private tothe block in which theyare declared. No partof the program outsideof the method / block can see them. Aspecial case is thatlocal variablesdeclared in theinitializer part of a
for
statement have a scopeof the
for
statement.Instance (field)variables can beenseen by all methods inthe class. Which other classes can see them isdetermined by their declared access:
private
should beyour default choice indeclaring them. Noother class can see private instancevariables. This isregarded as the bestchoice. Define
 getter 
and
 setter 
methods if the value has to begotten or set fromoutside so that dataconsistency can beenforced, and to preserve internalrepresentationflexibility.Default (also called
 package
visibility)allows a variable to beseen by any class inthe same package.
private
is preferable.
public
. Can be seenfrom any class.Generally a bad idea.Same as instancevariable, but are oftendeclared
public
tomake constantsavailable to users of theclass.
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...