Saturday, October 26, 2013

The scope and life time of variables

Lift Time of variable

So far, all of the variables used have been declared at the start of the main() method, However, java allows variables to be declared within any block,A block begins 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 creation a new scope.A scope determines what objects are visible to other parts of your program. it also determines the lifetime of those objects.

They are four type of variable's 

  1. Instance Variables
  2. Method Local Variables
  3. Block Variables
  4. Static Variables 
class Variables {

 
   static int high = 25;                           // static variable
   int with = 30;                                   // instance Variable
 
    public void myMethod(){
        double area = 100;                    // method local variable
        for (int i = 0; i < 10; i++) {
            int value = 10+i;                     // block variable
        }
    }
}
In java, the two major scopes are those defined by a class and those defined by a method. For now we will be discussing only the scope 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 include within the method scope. Parameters too work in the same way as any other method variable.
As a general rule, Variables declared inside a scope are not visible to code that is defined outside the scope, therefore , when you declare a variable within a scope, you are localizing that variable and protection it from unauthorized access and modification.

Scope can be nested eg : each time you create a block of code, you are creation a new , nested scope, when this occurs, the outer scope encloses the inner scope, this means that objects declared int he outer scope will be visible to code within the inner scope, However, the reverse is not true. Objects declared within the inner scope will not be visible outside it
class Scope {

    public static void main(String[] args) {
        int x; // visible to all code within main
        x =10;
        if(x==10){  // start new scope
            int y = 20; // visible onlly to this block
            // x and y both visible here
            System.out.println("X is : "+x+"  "+ "Y is : "+y);
        }
     
        y = 100;  // Error, variable y is not visible here
        // x still visible here
        System.out.println("X is :" +x);
    }
}
x is declared at the start of main() scope and is accessible to all subsequent code within the main().Within if block y is declared. Since a block defines a scope,Y is only visible to code within its block.This is why outside of its block,the line y = 100; is commented our Within the if block, x can be used because code within a block has access to variables declared by the outside scope.

Within a block,variables can be declared at any point but are valid only after they are declared, Therefore, if you declared a variable at the start of the code, All the code written after that will have access to that variable, However , if a variable is declared at the end of the code, the code written before the declaration will not have access to that particular variable.

Variables are created when their scope is entered and destroyed when their scope is left, This means a variable will not hold its value once it has one out of scope,Therefore, variables declared within a method will not hold their values between calls to that method also, a variable declared within a block will lose its value when the block is left, the lifetime of variable is limited its scope.

if a variable declaration includes an initializer , then that variable will be re- initialized each time the block in which it is declared is entered,

Example 


class LifeTiem {

    public static void main(String[] args) {
        int x;
        for (int i = 0; i < 3; i++) {
            int y = -1; // initialized each time block is enterd
            System.out.println("Y is : "+ y);
            y = 100;
            System.out.println("Y is Now : "+ y);
        }
    }

}
Output :
Y is : -1
Y is Now : 100
Y is : -1
Y is Now : 100
Y is : -1
Y is Now : 100

As your see, Y is always re-initialized to -1 each time the inner for loop is entered. Even though later in the code, it is entered the value 100 this value is lost

No comments:

Post a Comment