Write a program which show the effect of nested scopes.

Category : Java | Sub Category : Data type, Variable and Arays | By Runner Dev Last updated: 2020-09-28 15:02:08 Viewed : 210


(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:
bultify
at 2022-04-17 02:16:33
Fearuy Sale Isotretinoin Cheap Legally Mastercard Accepted https://bestadalafil.com/ - Cialis Huqmkm <a href="https://bestadalafil.com/">Cialis</a> Prix Du Levitra Orodispersible https://bestadalafil.com/ - Cialis Priligy Durchfall Mhkwun