next up previous
Next: Overloading of methods Up: Unit 03 Previous: Example: scope of local

Lifetime of local variables

The lifetime of a variable is the time during which the variable stays in memory and is therefore accessible during program execution.

The variables that are local to a method are created the moment the method is activated (exactly as formal parameters) and are destroyed when the activation of the method terminates.

More precisely, when the method is activated, a block of memory cells, called activation record, is allocated, which contains all local variables and formal parameters of the current method call. The activation record is used during the method execution, and is then removed at the end of the execution. When the activation record is removed, the memory locations for the local variables and for the formal parameters are destroyed and the values they contain are lost.

When the method is activated again, a new activation record is allocated, with new memory locations that have nothing to do with the ones of the previous activations. Hence, at each method activation the memory locations for local variables and formal parameters are created anew, and these memory locations are in general different from the ones of previous activations. It follows, that the values of local variables and of formal parameters are not kept from one method call to the next.

Note: The notion of lifetime of a variable is relevant at execution time.


next up previous
Next: Overloading of methods Up: Unit 03 Previous: Example: scope of local