Sie sind auf Seite 1von 3

c 


   
There are three kinds of Java variables:

1.› c     are declared in a method, constructor, or block. When a method is
entered, an area is pushed onto the   . 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.›     are declared in a class, but outside a method. They are also called
’ ’  or     . When an object is allocated in the  , 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.›
    are declared with the ?   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  and used as either public or private constants.

  
  
   
 
×   In a method, constructor, In a class, but outside a In a class, but outside a
    or block. method. Typically method. Must be
î  . declared ?  .
Typically also .
0 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 than 
 , variables that
method. 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.
 ’
  when method or
  when instance
  when the
constructor is entered. of class is created with program starts.
 . Π   when the
Destroyed on exit. Π   when there program stops.
are no more references to
enclosing object (made
available for garbage
collection).
£
 Local variables Instance (field) variables Same as instance
(including formal can been seen by all variable, but are often
parameters) are visible methods in the class. declared î to make
only in the method, Which other classes can constants available to
constructor, or block in see them is determined users of the class.
which they are declared. by their declared access:
Access modifiers
(î  , î, ...) î   should be your
can
 be used with default choice in
local variables. All local declaring them. No other
variables are effectively class can see private
private to the block in instance variables. This
which they are declared. is regarded as the best
No part of the program choice. Define ½   and
outside of the method /    methods if the
block can see them. A value has to be gotten or
special case is that local set from outside so that
variables declared in the data consistency can be
initializer part of a 
enforced, and to preserve
statement have a scope of internal representation
the 
statement. flexibility.

Default (also called


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

î. Can be seen


from any class. Generally
a bad idea.

î
  variables are
only visible from any
descendant classes.
Uncommon, and
probably a bad choice.
Π  
Declare before use Declare anywhere at Declare anywhere at
anywhere in a method or class level (before or class level with ?  .
block. after use).
Y    None. Must be assigned Zero for numbers, false Same as instance
a value before the first for booleans, or null for variable, and it addition
use. object references. May can be assigned value in
be assigned value at special     
declaration or in 
.
constructor.
 
’ Impossible. Local Instance variables should Class variables are

 variable names are be declared î   to qualified with the class
known only within the promote information name (eg, Color.BLUE).
method. hiding, so should not be They can also be
accessed from outside a qualified with an object,
class. However, in the but this is a deceptive
few cases where there are style.
accessed from outside
the class, they must be
qualified by an object
(eg, myPoint.x).
Ñ ’    Standard rules. Standard rules, but are ?  î
often prefixed to clarify variables (constants) are
difference from local all uppercase, otherwise
variables, eg with ’ , ’, normal naming
or ’ (for member) as in conventions.
’  , or ? as in Alternatively prefix the
? . variable with "c_" (for
class) or something
similar.
›

Das könnte Ihnen auch gefallen