Saturday 11 March 2017

Variables scope program in java


Scope of a variable is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e.scope of a variable can determined at compiler time and independent of function call stack.

Program:

public class VarScopeProg
{
 public static void main(String[] args)
 {
   int A=10;
   {
     int B=20;
     System.out.println("Block A: "+A);
     System.out.println("Block B: "+B);
   }//scope of B=20 ends here
   {
   int B=30;//scope of B=30 starts here
   System.out.println("Block A: "+A);
   System.out.println("Block B: "+B);
   }//scope of B=30 ends here
   int B=40;//scope of B=40 starts here
   {
     System.out.println("Block A: "+A);
     System.out.println("Block B: "+B);

   }
 }//scope of all the variables end here
}

No comments:

Post a Comment