Demonstrate lifetime of a variable : Variables « Language « Java Tutorial






public class MainClass {
  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 is: " + y); // this always prints -1
      y = 100;
      System.out.println("y is now: " + y);
    }
  }
}
y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100








1.9.Variables
1.9.1.Variables
1.9.2.Variable Dynamic Initialization
1.9.3.Demonstrate lifetime of a variable
1.9.4.shows the proper way to declare a class variable named helloMessage
1.9.5.You don't have to place class variable declarations at the beginning of a class.
1.9.6.helloMessage variable is declared as a local variable: