Local Variable Calculation Examples
.
Summary Examples of computing the required number of local variables

foo uses 2 literal slots and 1 object slot

  int foo( int a ){
    return a;
  }

foo2 uses a maximum of 8 literal slots and 2 object slots

  int foo2( int a, int b, int[] c )
    int total = 0;


        for(int i = a; i < b; i++){
          total+=c[i];        
        }


    return total;
  }

foo3 uses a maximum of 10 literal slots and 1 object slots. The reason is that as local variables move out of scope they become re-used. As a static method there is no this object

  static int foo3( int a, int b, int[] c )
        int total = 0;


        for(int i = a; i < b; i++){
          total+=c[i];        
        }


        int max = 0;                        // Re-uses slot from a
        int min = 10000;                         // Re-uses slot from b                        


        for(int j = 0; j < c.length; j++){   // Re-uses slot from i
           int val = c[j];                         // New Slot allocated.
           if( max > val ){
              max = val;
           }
           if( val < min ){
              min = val;
               }
        }


        String result = new String( total );  //Re-uses slot from c[]


        //Uses operand stack not local variables for storage
        System.out.println( "Result = " + result + " Max = " + String.valueOf(max) + " Min = " + String.valueOf( min ) );


        return total;
  }