You are on page 1of 3

Variable lifetime

variable lifetime (when is the variable allocated memory and initialized, and when is
that memory free

When do variables come to life, and how long do they live? For RPG III, as you know,
they come to life when the program is first called and they live until the program is ended
(by either setting the LR indicator to 1 or by ending the job). Global fields in RPG IV
have the same lifetime, although you can now subdivide a job into named ILE activation
groups, and fields will not live past the life of these. The new local fields inside RPG IV
procedures only come to life when the procedure is called, and they die immediately
when it ends. These are sometimes called automatic fields, since their death is automatic
with the ending of the procedure. However, if you specify STATIC for a local field in
RPG, that field's value persists for the life of the program or service program it is in.

For Java methods and nested blocks inside methods, these automatic or local variables
come to life when the method is called or the block is entered. If an initialization has been
specified, it is applied at that time. When the block ends or method ends, the variable
goes away, just as with RPG IV procedures. (By come to life we really mean when the
variables get allocated actual storage in memory.)

For Java class-level variables, life is more interesting. Non-static class-level variables
(that is, instance variables) come to life when an instance of the class (object) is
instantiated with the new operator. Each class instance gets its own memory allocation
for the instance variables, so they are totally independent of each other. If your instance
variable declaration includes an initializer (for example, = 10), that initial value is applied
at the same time as the object is created, and its instance variables come to life.

For static class-level variables (that is, class variables), there is only one allocated
memory location per variable, regardless of the number of instances of the class. Since
the life and values of a static variable are not dependent on, or even related to, instances
of the class, it makes sense that static variables come to life or are allocated memory as
soon as the Java runtime "knows about" the class. This typically occurs at pre-runtime,
when the application is starting up, as it does for RPG global fields. The important thing
to remember is that static variables will be alive as soon as you first possibly need them.
Recall that in Java, unlike RPG, local variables in a method cannot be static.
Well, they are similar in that local variables have a narrower scope than instance
variables, and instance variables have a narrower scope than static variables.

However, there is still a very big difference between local variables and everything else:
the lifetime of a local variable is determined by execution path and local variables live on
the stack, while instance and static variables are associated with objects and therefore
live in the heap.

Another big difference is that both instance and static variables ALWAYS have a value.
If your code does not give them a value then the run-time system guarantees that they'll
receive a value of null/0/0.0/false. For a local variable, it is illegal for code to fail to
assign it a value.
Lifetime
LOCAL
Created when method or constructor is entered.
Destroyed on exit.

INSTANCE
Created when instance of class is created with new.
Destroyed when there are no more references to enclosing object (made available for
garbage collection).

CLASS
Created when the program starts.
Destroyed when the program stops.

You might also like