So here is a chart summarizing what we've learned so far about variables;
Variable | Where Declared | Where Stored | Lifetime | Scope | Use | ||
---|---|---|---|---|---|---|---|
Local Var. | Method Body | Run-time stack | Duration of same method | Declaration -> end of enclosing block | Intermediate value used in method | ||
Method Parameter | Method declaration | Run-time stack | Duration of same method | Entire body of same method | Transmit value into method | ||
Instance Var. | Class definition | Object | Lifetime of object |
|
Store property of object | ||
Static Var. | Class definition | Static space | Duration of program |
|
Store property of class as a whole |
So local variables and method parameters are quite similar; they really only differ in where they are declared, and the extent of their scope. Instance variables and static variables are similar in that they are both declared in class definitions. However, because one is stored with each instantiated object in object space and the other with the class definition in static space, the first can have many versions (one for each object) and the second refers to some property of the class as a whole.