Here is a Java Program to Demonstrate Lifetime of a Variable.
Output of Above Java Program
y iz: -1
y is now: 100
y iz: -1
y is now: 100
y iz: -1
y is now: 100
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
System.out.println("y iz: " + y); // this always prints -1
y = 100;
System.out.println("y is now: " + y);
}
}
}
Output of Above Java Program
y iz: -1
y is now: 100
y iz: -1
y is now: 100
y iz: -1
y is now: 100