next up previous
Next: Shadowing: When Scopes Collide Up: Varieties of Variables Previous: Static or Class Variables

Variable Summary

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
private: All methods in class
public: Package that contains class, packages that have access to this package
Store property of object
Static Var. Class definition Static space Duration of program
private: All methods in class
public: Package that contains class, packages that have access to this package
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.


next up previous
Next: Shadowing: When Scopes Collide Up: Varieties of Variables Previous: Static or Class Variables
Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09