Scope of Variables In Java - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Thursday, November 1, 2018

Scope of Variables In Java

Scope of variables in Java

Scope of a variable is part of the program where a variable is accessible. The scope of the variable is defined at the compile time and independent of method call stack.

A block is begun with an opening curly brace and ended by a closing curly brace. A block defines a scope. Thus, each time you start a new block, you are creating a new scope. A scope determines what objects are visible to other parts of your program.

In Java, the two major scopes are those defined by a class and those defined by a method.

The scope defined by a method begins with its opening curly brace. However, if that method has parameters, they too are included within the method’s scope.

As a general rule, variables declared inside a scope are not visible (that is, accessible) to code that is defined outside that scope.

Example of block scope

Scope can be nested. When you create a block of code, you are creating a new, nested scope. When nested scope created, the outer scope encloses the inner scope. That means the object declared in outer scope will be accessible to the code within inner scope. However, the reverse is not true.

// demonstrate block scope
public class ScopeDemo {
  public static void main(String[] args) {
    int x = 10; // this variable will accessible within main

    if (x == 10) { // start a new scope
      int y = 20; // know only to this scope

      // x and y both are accessible in this scope
      System.out.println("Value of x and y: " + x + " " + y);
      x = y * 5;
    } // inner scope or new scope end here

    // y=100; // Error!! y is not know here

    // x is still know here
    System.out.println("Value of x: " + x);
  } // end of main scopee
}

The variable x is declared at the start of the main( )'s scope and it is accessible to all subsequent of the code within the main method. Now a new variable y is declared inside if block and it is accessible inside if block only but not accessible to main scope or outside if block.

Variable x can be used because code in inner block has access to variables declared by an outer block.

Output of the program:

Value of x and y: 10 20
Value of x: 100

Variables are created when their scope is entered and destroyed when their scope is left. The lifetime of a variable is confined to its scope.

Let's understand block scope with one more example using the for loop. If the variable declaration includes an initializer inside loop, then variable will reinitialized each time when enter into the block.

public class LifeTimeDemo {
  public static void main(String[] args) {
    int i;
    for (i = 0; i < 3; i++) {
      int x = -1; // x is initialized each time block is entered
      System.out.println("Value of x: " + x); // this always prints -1
      x = 10;
      System.out.println("Now value of x: " + x);
    }
  }
}

Output of the program:

Value of x: -1
Now value of x: 10
Value of x: -1
Now value of x: 10
Value of x: -1
Now value of x: 10

As you can see by the output of the program, each time y is reinitialized to -1 when for loop entered. Even though it is subsequently assigned the value 10, this value is lost.

One last point: Although blocks can be nested, you cannot declare a variable to have the same name as one in an outer scope.

public class ScopeErrDemo {
  public static void main(String args[]) {
    int x = 1;

    {
      int x = 2; // compile time error
    }
  }

Above program generate compile time error, because you are trying to declare a variable x inside the new inner block.

No comments :

Post a Comment