Write a program which show the effect of nested scopes.

Category : Java | Sub Category : Data type, Variable and Arays | By Prasad Bonam Last updated: 2020-09-28 09:32:08 Viewed : 472


(Data type, Variable and Arays)

Write a program which show the effect of nested scopes.

DESCRIPTION:This program shows the scope of any variable. As the comment indicate we Ist declare the class and then initialize and delare


Source Code:  Scope.java


 

 /**

 *

 * .Write a program which show the effect of nested scopes.

 */

 

class Scope { // This line daclare a new class & the class defination is start.

     public static void main(String a[]) { // This line begins the main() method.

         int x; // initialize x

         x = 10; // Declare value of x.

         if (x == 10) { // start new scope

              int y = 20; // this y is known only to this block.

              System.out.println("x and y : " + x + " " + y); // This line show the output

              x = y * 2; // this line show x equals to y*2

 

         } // this line show the finishing of block

         // y=100; // error! y not known here.

         System.out.println("x is " + x);// this line show the output

     } // this line show the end of program

} // this line show the end of class


OUTPUT:





Search
Related Articles

Leave a Comment: