showscope.java

来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 28 行

JAVA
28
字号
public class ShowScope
{
  private static int firstnum;  // this has class scope
                    
    // all variables in this method have block scope
  public static void main(String[] args)
  {
    int secnum;          // create a local variable named secnum
    
    firstnum = 10; // store a value into the class variable
    secnum = 20;   // store a value into the local variable
    System.out.println("From main(): firstnum = " + firstnum);
    System.out.println("From main(): secnum =  " + secnum);
    changeValues();      // call the method changeValues
    System.out.println("\nFrom main() again: firstnum = " + firstnum);
    System.out.println("From main() again: secnum = " + secnum);
  }
      // all variables declared in this method have block scope
  public static void changeValues()   // no values are passed to this method
  {                                   // and no value is returned
    int secnum;  // create a local variable named secnum
    secnum = 30; // this only affects this local variable's value
    System.out.println("\nFrom changeValues(): firstnum = " + firstnum);
    System.out.println("From changeValues(): secnum = " + secnum);
    firstnum = 40;    // this changes firstnum for both methods
  }
}  // end of class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?