Source of Scope.java


  1: // Fig. 6.11: Scope.java
  2: // Scope class demonstrates field and local variable scopes.
  3: 
  4: public class Scope
  5: {
  6:    // field that is accessible to all methods of this class 
  7:    private int x = 1; 
  8: 
  9:    // method begin creates and initializes local variable x 
 10:    // and calls methods useLocalVariable and useField
 11:    public void begin()
 12:    {
 13:       int x = 5; // method's local variable x shadows field x
 14: 
 15:       System.out.printf( "local x in method begin is %d\n", x );
 16: 
 17:       useLocalVariable(); // useLocalVariable has local x
 18:       useField(); // useField uses class Scope's field x
 19:       useLocalVariable(); // useLocalVariable reinitializes local x
 20:       useField(); // class Scope's field x retains its value
 21: 
 22:       System.out.printf( "\nlocal x in method begin is %d\n", x );
 23:    } // end method begin
 24: 
 25:    // create and initialize local variable x during each call
 26:    public void useLocalVariable()
 27:    {
 28:       int x = 25; // initialized each time useLocalVariable is called
 29: 
 30:       System.out.printf( 
 31:          "\nlocal x on entering method useLocalVariable is %d\n", x );
 32:       ++x; // modifies this method's local variable x
 33:       System.out.printf( 
 34:          "local x before exiting method useLocalVariable is %d\n", x );
 35:    } // end method useLocalVariable
 36: 
 37:    // modify class Scope's field x during each call
 38:    public void useField()
 39:    {
 40:       System.out.printf( 
 41:          "\nfield x on entering method useField is %d\n", x );
 42:       x *= 10; // modifies class Scope's field x
 43:       System.out.printf( 
 44:          "field x before exiting method useField is %d\n", x );
 45:    } // end method useField
 46: } // end class Scope
 47: 
 48: /**************************************************************************
 49:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 50:  * Pearson Education, Inc. All Rights Reserved.                           *
 51:  *                                                                        *
 52:  * DISCLAIMER: The authors and publisher of this book have used their     *
 53:  * best efforts in preparing the book. These efforts include the          *
 54:  * development, research, and testing of the theories and programs        *
 55:  * to determine their effectiveness. The authors and publisher make       *
 56:  * no warranty of any kind, expressed or implied, with regard to these    *
 57:  * programs or to the documentation contained in these books. The authors *
 58:  * and publisher shall not be liable in any event for incidental or       *
 59:  * consequential damages in connection with, or arising out of, the       *
 60:  * furnishing, performance, or use of these programs.                     *
 61:  *************************************************************************/