Skip to main content

Block Scope Variable in Java

A block scope refers to variables that are declared within a pair of curly braces {}, such as within methods, loops, or conditionals

Demonstration of block scope variable:

   int x; // known to all code withing block global declaration

  if(x==10) {

  // start new scope

 int y=20;

/ / y known only to this block

// x and y both here can be used

  System.out.println("x and y" + x and "" +y);

  x=y*2;

}

// y =100/ Error! you can't call y here because out of scope here

// x  is still known here

 System.out.println("x is "+x);

                       }

}|