Review : Local variables and parameter variables

  • A local variable is a variable that is defined inside a method body

  • A parameter variable is a variable that is defined inside a method header

  • Recall:

    • (All) variables have a scope and a life time (not covered in AP Comp Sc)

Block

  • A block is defined by matching curly braces { ... }

    Example:

        public static void main(String[] args) 
        {  // block 1
           int i, sum;
           
           sum = 0;
           i = 0;
           while ( i < 10 )
           {  // block 2
              sum = sum + i;
    	  i++;
           }
        } 

  • Blocks in programming languages are closely related to the scope concept

The scope of local variables

  • Scope:

    • The scope of a variable is the part of the program where the variable can be used/accessed


  • The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable:

The scope of local variables    --    example 1

  • The scope of the variable a in the program is highlighted:

          public static void main(String[] args) 
          {
              int a = 4;          // Line 1 - start of the scope of a
                                  
              {                   
                                  
                  int x = 4;      
                                  
                  x = x + 1;      
                  a = 7;          
              }                   
                                  
              a = 5;		    
          }			    // Line 2 - end of the scope of a

  • When you run this program in BleuJ, the variable a will:

      • Appear when the program reaches line 1 (now you can use a)
      • Disappear when the program reaches line 2 ( a cannot be used!)

DEMO: demo/06-methods/06-scoping/Scoping1.java

The scope of local variables    --    example 2

  • The scope of the variable x in the program is highlighted:

          public static void main(String[] args) 
          {
              int a = 4;
      
              {
      
                  int x = 4;      // Line 1 - start of the scope of x
                                  
                  x = x + 1;      
                  a = 7;          
              }                   // Line 2 - end of the scope of x
       
              a = 5;
          }

  • When you run this program in BleuJ, the variable x will:

      • Appear when the program reaches line 1 (now you can use x)
      • Disappear when the program reaches line 2 ( x cannot be used!)

DEMO: demo/06-methods/06-scoping/Scoping1.java

The scope of local variables   it is illegal to use a variable outside its scope

  • You cannot use the variable x outside its scope:

          public static void main(String[] args) 
          {
              int a = 4;
      
              {
      
                  int x = 4;      // Line 1 - start of the scope of x
                                  
                  x = x + 1;      
                  a = 7;          
              }                   // Line 2 - end of the scope of x
              x = 5;   // Illegal: x used outside its scope
              a = 5;
          }

  • When you compile this program in BleuJ, you will get this compile error:

      Undeclared variable x
    

DEMO: demo/06-methods/06-scoping/Scoping2.java

The for-loop:   creating a local (index) variable in a for-loop

  • Java has a special syntax to define a for-loop index variable as a local variable that has the for-loop body as its scope:

          public static void main(String[] args) 
          {
              int sum = 0;
      
      	for ( int i = 0; i < 10; i++ )  // Line 1: defines var i
              {                               
                  sum = sum + i;              
              }                               // Line 2
      
              System.out.println(sum);
          }

  • When you run this program in BleuJ, the variable i will:

      • Appear when the program reaches line 1 (now you can use i)
      • Disappear when the program reaches line 2 ( i cannot be used!)

DEMO: demo/06-methods/06-scoping/ForLoopIndex.java

The scope of local variables   creating a local index variable in for-loop

  • Common error in using for-loop index variable:
     

          public static void main(String[] args) 
          {
              int sum = 0;
      
      	for ( int i = 0; i < 10; i++ )  // Line 1: defines var i
              {                               
                  sum = sum + i;              
              }                               // Line 2
      
              System.out.println( i );  // Error: used i outside its scope
          }

  • You will get a compile error:

      • Undeclared variable i

DEMO: demo/06-methods/06-scoping/ForLoopIndex.java

The scope of parameter variables

  • Recall: scope:

    • The scope of a variable is the part of the program where the variable can be used/accessed


  • The scope of a parameter variable starts from beginning of the method and continues to the end of the method:

    Note:   A parameter variable behaves like a local variable defined at the start of a method

  Block configurations  

  • 2 different blocks can have the following configurations:

    • Disjoint:   blocks do not have any no overlapping area

        { // Block 1
           ...
        }
                           
        {
          // Block 2
           ...
        }
      

    • Nested:   one block is contained by another block

        { // Block 1
           ...
           {
              // Block 2
              ...
           }
        }

Scoping rules for non-intersection blocks

  • Scoping rule for variables that are defined in non-intersecting blocks:

    • The same variable name can be defined in different non-intersecting blocks

    • They will define different variables

    Example:   the variable i in these 2 non-intersecting blocks are different variables

        public static void main(String[] args) 
        { // Block 1
            double sum;
    
    	sum = 0;
            for ( int i = 1; i <= 4; i++ )     
            { // Block 2                       
    	   sum = sum + i;                  
    	}                                  
    
    	sum = 0;
            for ( double i = 1; i <= 1.4; i = i + 0.1 )  
    	{ // Block 3                                 
    	   sum = sum + i;                            
            }                                            
        }

DEMO: demo/06-methods/07-block-conf/NonIntersectBlks.java    --    Show i has different data type !

 Java's scoping rules for nested blocks  

  • Scoping rule for variables that are defined in nested blocks:

    • A variable name defined in an outer block cannot be defined in any inner block     

      (This is different in other programming languages)

    Example:   the variable i cannot be defined again inside block 2:

        public static void main(String[] args) 
        { // Block 1
    
            double sum;
    
    	sum = 0;
            for ( int i = 1; i <= 4; i++ ) 
            { // Block 2
    
               for ( double i = 1; i <= 1.4; i = i + 0.1 ) 
    	   { // Block 3 error: variable i defined in outer block
    	      sum = sum + i;
               }
    	}
        }

DEMO: demo/06-methods/07-block-conf/NestedBlks.java

  Life time of parameter and local variables  

  • Life time of a variable:

    • Life time of a variable = the duration/time frame that the variable exists in memory


  • Background information:

    • Parameter variables are created when the method is invoked (they are used to pass arguments)

    • Local variables are also created when the method is invoked (they are used to help you code the method)


    • Parameter and local variables are de-allocated (= removed from memory) when the method returns (= exits)


  • Life time of parameter and local variables:

    • The duration from the start of the method invocation until the method returns (= exits)