Scope for Class and Local Variables : Class Fields « Class Definition « Java Tutorial






public class MainClass {
  static int x;

  public static void main(String[] args) {
    x = 5;
    System.out.println("main: x = " + x);
    myMethod();
  }

  public static void myMethod() {
    int y;
    y = 10;
    if (y == x + 5) {
      int z;
      z = 15;
      System.out.println("myMethod: z = " + z);
    }
    System.out.println("myMethod: x = " + x);
    System.out.println("myMethod: y = " + y);
  }
}








5.4.Class Fields
5.4.1.Scope class demonstrates field and local variable scopes
5.4.2.Dynamically changing the behavior of an object via composition (the 'State' design pattern)
5.4.3.Scope for Class and Local Variables
5.4.4.A Class that Demonstrates Shadowing