Write a program in Java to show scope of variables
class scope {
public static void main(String arg[]) {
int x; //known to all code within main function
x=10;
if(x==10) {
//Start new scope
int y=20; //known only to this block
//x and y both known here
System.out.print("x and y : "+x+" "+y);
x=y*2;
}
//y=100; //error because y is not here
//x is still known here
System.out.print("\nx is "+x);
}
}
Post a Comment