The following table summarizes the different variable treatments:
Name | Nature | Scope | Lifetime | Initialization | Example |
Class instance variables | one copy of each variable for each instance of that class - declared in class, outside any method | generally, the class and its children | as long as the instance is alive | Yes - default values given for primitive data types, numbers are 0 or 0.0 and booleans are false | a, b, c, sa, sb, sc, tt, ft |
Class variables | only one copy of each variable for the whole class - declared "static" in class, outside any method | generally, the class and its children | from the time that any identifier of that class is referenced, i.e., the class is loaded at run time, until the program quits, generally | As class instance | classCount |
Class final variables | may be "class instance" or just "class variable", generally should be "static", may not be changed or redefined by any children of the class, normally all uppper case for easy identification | as any class variable or class instance variable, as declared | as class or class instance, as declared | Yes, but normally explicity initialized, may not be changed after initialization in initial declaration | (none here) |
Method local variables | declared inside a method | only within the method, may be passed to other methods in parameter lists | as long as the original method is active | No | (main): left, top, dd, ff, ig, tx
(mySum): sum |
Method formal parameters | declared inside a method, as method variables | as method variables | as method variables | Initialized by caller | (main): args - array of String
(mySum): n, m |
Block variables | declared inside a block or in the "For Init" part of a for loop (pg 608) | from when the variable is declared until the block ends with a curly bracket | ends when the block ends | No | ty in for loop ty in block |
Special notes:
Example:
public class TestOne
{
int a, b, c = 3;
String sa, sb="", sc="some
string";
static int classCount = 0;
public static void main (String[]
args)
{
int left, top;
double dd =
17.5;
float ff =
(float) 17.5; // error without the (float) cast
int ig = 3;
static int
methodCount = 0; //
illegal inside a method
if (dd < 30)
ig = 3;
ig = ig + 3;
for (left = 0;
left < 10; left++) {
top = top = 5;
System.out.println ("Top: " + top);
} // end for
for (int ty =
0; ty < 10; ty++) {
top = ty - 7;
} // end for
{
int ty = 7;
top = ty + 3;
} // end block
top = ty +
7; // this is an error, ty not defined here
// the following tests static vs
non-static references
// since main is static
// generally, tx indicates the
class context below
TestOne tx =
new TestOne ();
tx.tt = 37.3;
// ok because tx gives instance
reference
tt =
49.6;
// error, static
reference to instance
variable
mySum (5,
7); // error, static reference
to instance
method
tx.mySum
(5,7); // ok becuase tx gives instance
reference
TestOne.classCount++; //
class context explicit
} // end method main
double tt = 3.7;
float ft =
4.6f; // error without the f
public int mySum (int n, int m) {
int sum;
if ((m>0)
&& (n>0)) {
sum = 0;
while (n != 0) {
sum = sum + n*m;
n--;
} // end while
return sum;
} // end if
else
return 0;
} // end method mySum
} // end class TestOne