Duration of Identifiers

An identifier's duration (or lifetime) is the period during which that variable exists in memory. Some identifiers exist briefly. Others are repeatedly created and destroyed. Still others exist for the entire execution of the program.

automatic duration

Identifiers representing local variables within a method have automatic duration. They are created when program control reaches their declaration (by extending the stack). They exist while the block in which they are declared is active and they are destroyed when the block in which they are declared is exited.

static duration

Identifiers labeled as static exist from the point at which the class that defines them is loaded into memory for execution until the program is terminated.
public class Duration extends JApplet {
		static int count = 0;  // static variable (only one copy)
		int total = 0;         // instance variable (copy in each instance)

	public void start()
    {
		MethodA();
		MethodA();
    }
    
    public void MethodA()
    {
		int sum = 0;           // local variable

		sum += 5;
		total += sum;
		count++;
		System.out.println("count " + count + "  sum " + sum + "  total " + total);
    }
}


Maintained by John Loomis, last updated 1 June 2000