Program:- Variable Scope
class Scope
{
public void ScopeTest()
{
int x; // known to all code withing this function
x=10;
if (x==10)
{ //start new scope
int y = 20 ; //known only to this block
x = y * 2 ;
}
y=100; //Error!! y not known here
//x is still known here.
System.out.println("x is " + x); // No problem here
//Hence the complier complains if I refernce a variable out of scope
}
}
No comments:
Post a Comment