Variable Scope : variable scope « Java Source And Data Type « SCJP






class Layout {                      

  static int s = 343;               // static variable

  int x;                            // instance variable

  { x = 7; int x2 = 5; }            // initialization block

  Layout() { x += 8; int x3 = 6;}   // constructor

  void doStuff() {                  // method

    int y = 0;                      // local variable

    for(int z = 0; z < 4; z++) {    // 'for' code block
      y += z + x;
    }
  }
}

Static variables have the longest scope.
Static variables are created when the class is loaded.
Instance variables are created when a new instance is created, and they live until the instance is removed.
Local variables live as long as their method remains on the stack. 
Block variables live only as long as the code block is executing.








1.26.variable scope
1.26.1.Variable Scope
1.26.2.Attempting to access an instance variable from a static context.
1.26.3.Attempting to access a local variable from a nested method.
1.26.4.Attempting to use a block variable after the code block has completed.
1.26.5.Primitive and Object Type Instance Variables
1.26.6.Initialization Blocks