Scope of LOCAL & PARAMETER variables in Java/C/C++

  1. The ALGOL 60 programming language was first langauge to introduce the scoping concept:

    In Java,C,C++: { } define one level of scoping
         .....
          { (Outer scope)
    	....
    	....
    	{ (Begin of a NEW scope)
    	  (Inner scope)
    	  ....
    	  ....
    	}
    	....
    	....
          }
    

  2. In Java, C & C++, the braces ({, }) starts a new scope

    Example: inner scope inherits variables from outer scope
          public class myClass
          {
             public void myMethod(....)
    	 {
    	    int x;
    	    ... // int x is accessible after its definition   
    	    ...
    	    while (....)
    	    { Start of new scope...
    	       ...
    	       ... // int x is ALSO accessible in new scope
    	       ...
    	       ...
    	    }
    	 }
          }
    

  3. The new scope INHERITS all variables from its surrounding outer scope and may SHADOW some of the inherited variables

  4. Shadowing: When the a variable of the SAME name is defined in the INNER scope, the variable (of the SAME name) in the OUTER scope is SHADOWED.

    The SHADOWED variable (in the outer scope) is NOT accessible in the INNER scope

  5. Examples...

    Example 1: local variable shadowing an instance variable
          public class myClass
          {
    	 int x;
    
             public void myMethod(...)
    	 {
    	    ... // the name "x" in this part refers to int x
    	    ... 
    	    double x;
    	    ...
    	    ... // the name "x" in this part now refers to double x    
    	    ...
    	    ...
    	 }
          }
    
    • NOTE: there is a trick to access int x in the magenta section of the program...

    Example 2: local variable shadowing a parameter variable
          public class myClass
          {
             public void myMethod(int x)
    	 {
    	    ... // the name "x" in this part refers to int x
    	    ...
    	    if (.....)
    	    {
    	       ... // the name "x" in this part still refers to int x
    	       ...
    	       double x;
    	       ...
    	       ... // the name "x" in this part now refers to double x    
    	       ...
    	       ...
    	    }
    	    ...
    	    ...
    	 }
          }
    

    Example 3: one local variable shadowing another local variable
          public class myClass
          {
             public void myMethod(....)
    	 {
    	    int x;
    	    ... // the name "x" in this part refers to int x
    	    ...
    	    while (....)
    	    {
    	       ... // the name "x" in this part still refers to int x
    	       ...
    	       double x;
    	       ...
    	       ... // the name "x" in this part now refers to double x    
    	       ...
    	       ...
    	    }
    	 }
          }
    



  6. Scope of Local variables:

    Example 1: local variable
          public class myClass
          {
             public void myMethod(...)
    	 {
    	    ...
    	    ...
    	    int x;  // <-- Local variable   
    	    ...
    	    ...
    	 }
          }
    

    Example 2: local variable
          public class myClass
          {
             public void myMethod(...)
    	 {
    	    ...
    	    if ( ... )
    	    {  
    	       ....
    	       ....
    	       int x;  // <-- Local variable   
    	       ....
    	       ....
    	    }
    	 }
          }
    

    Example 3: local variable
          public class myClass
          {
             public void myMethod(...)     
    	 {
    	    ...
    	    ... 
    	    int x;          // x & y are local variables
    	    BankAccount y;
    	    ...
    	    ...
    	    if ( ... )
    	    {  
    	       ....
    	       ....
    	       double x;       // overshadows x & y
    	       int y;
    	       ....
    	       ....
    	    }
    	    ....
    	    ....
    	 }
          }
    

  7. Scope of Parameter variables:

    Example: parameter variable
          public class myClass
          {
             public void myMethod(int x)    
    	 {
    	    ...
    	    ...
    	 }
          }
    

    Example: parameter variable
          public class myClass
          {
             public void myMethod(int x)    
    	 {
    	    ......
    	    ......
    	    if (....)
    	    {
    	       ......
    	       ......
    	       double x;
    	       ......
    	       ......
    	    }
    	    ......
    	    ......
    	    ......
    	 }
          }
    

    Conclusion: A parameter variable behaves the same way as a local variable defined at the beginning of a method .

    If is looks like a duck, quacks like a duck... well ?