/** This program demonstrates some basic features of scope. Scope defines
 *  the region in a program where a variable exists and can be used.
 *
 *  @author TK Rogers
 *  @version 10-26-10 */
public class ScopeDemo {
   private int x = 2; // Creates a field called x with a scope inside the
                            // ScopeDemo class.
   public static void main ( ) {
       ScopeDemo s = new ScopeDemo ( ); // Creates a local instance with a      
                                                                // scope inside main.
       int x = 12; // Creates a local variable called x with a scope inside main
       System.out.println ( "In main instance x = " + s.x + "  local main x = " + x );
       s.alter ( );
       s.x = 23;
       s.alter ( );
       System.out.println ( "In main instance x = " + s.x + "  local main x = " + x );
 
       { // This pair of brackets could be attached to a statement such as a 
         // for-loop or if statement. Any variable created here will only have 
         // a scope inside the brackets.
           int z = 500; // Creates a local variable called z with a scope inside the { }
           System.out.println ( "In main instance {z} = " + z );
       } // At this point the variable z ceases to exist.
      
       // Try uncommenting the line shown below and the program
 //  will not compile. Why?
       // System.out.println ( "In main instance z = " + z );
   }
  
   public void alter ( ) {  
       System.out.print ( "In alter instance x = " + x );
       int x = 70; // Creates a local variable called x with a scope inside alter
       System.out.println ( "  local alter x = " + x );
      
       // Try uncommenting the line shown below and the program
 // will not compile. Why?
       // System.out.print ( "instance x = " + s.x );
   }
}