Introduction to Variable Scope Tutorial

A variable is a named memory location that can be assigned a value while the program is running. Access to variables depends a great deal on where the variable is first created. This tutorial will focus on variables created inside of a method body or as parameters in the method signature. Variable scope will determine what parts of your program have access to certain variables. In many previous tutorials, I have referred to the area inside of an opening and closing curly brace as the code block. Variables created inside of these code blocks cannot be accessed outside of the code block. Control flow statements like the if and for statements, typically have code blocks directly following the closing parenthesis. Nested code blocks are called inner code blocks. The nested code blocks have access to the variables created in the outer code blocks but not vice versa.
(type) (methodName) ((parameters)) { // begin method body
      int a = 0; // visible to entire method
      { // begin nested code block #1
            a++; // a is visible in code block #1
            int b = 0; // create b in code block #1

            { // begin nested code block #2
                a++; // a is visible in code block #2
                b++; // b is visible in code block #2
                int c = 0; // create c in block #2
                c++; // c is visible in code block #2
            } // end nested code block #2

            a++;
            b++; // c is now outside of our scope
      } // end nested code block #1

      a++; // b and c are outside of the scope
} // end method body



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md VariableScope
C:\Java>cd VariableScope
C:\Java\VariableScope>Notepad VariableScope.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class VariableScope {
    public static void main(String args[]) {
        int a = 0; // visible to entire method
        { // begin nested code block #1
            a++; // a is visible in code block #1
            int b = 0; // create b in code block #1

            { // begin nested code block #2
                a++; // a is visible in code block #2
                b++; // b is visible in code block #2
                int c = 0; // create c in block #2
                c++; // c is visible in code block #2
                System.out.println("in code block #2, a = " + a);
                System.out.println("in code block #2, b = " + b);
                System.out.println("in code block #2, c = " + c);
                if (args.length >= 1) {
                    System.out.println("method parameters are visible in every inner code block, args[0] = " + args[0]);
                }
            } // end nested code block #2

            a++; 
            b++; // c is out of scope            
            System.out.println("in code block #1, a = " + a);
            System.out.println("in code block #1, b = " + b);
            //System.out.println("in code block #1, c = " + c); // c is out of scope
        } // end nested code block #1

        a++; // b and c are out of scope
        System.out.println("in method body, a = " + a);
        //System.out.println("in method body, b = " + b); // b is out of scope
        //System.out.println("in method body, c = " + c); // c is out of scope
        
        System.out.println();
        if (a > 3) { // another nested code block
            System.out.println("if statement code block, a = " + a);
            int d = 6;
            System.out.println("if statement code block, d = " + d);
        }

        System.out.println();
        for (int i = 0; i < 4; i++) { // another nested code block, i is created in the for statement
            System.out.println("for statement code block, a = "+ a + ", i = " + i);
        }

        // System.out.println("in method body, d = " + d); // d is out of scope
        // System.out.println("in method body, i = " + i); // i is out of scope
    }
}

Now switch back to the command prompt (CMD) and type in javac VariableScope.java and press Enter.
Now type in java VariableScope and press Enter.
Add some arguments when invoking the VariableScope class, see what happens.


C:\Java\VariableScope>javac VariableScope.java
C:\Java\VariableScope>java VariableScope
in code block #2, a = 2
in code block #2, b = 1
in code block #2, c = 1
in code block #3, a = 4
in code block #3, b = 2
in method body, a = 4

if statement code block, a = 4
if statement code block, d = 6

for statement code block, a = 4, i = 0
for statement code block, a = 4, i = 1
for statement code block, a = 4, i = 2
for statement code block, a = 4, i = 3


Final thoughts


Tutorials