The following Java code does not compile.
int a = 0;
if(a == 1) {
int b = 0;
}
if(a == 1) {
b = 1;
}
Why? There can be no code pa
variable b's scope is only until the if block completes, as this is where you declared the variable. That is why it cannot be accessed on the following block. This is for memory allocation, otherwise they would be ALOT of variables floating around in the memory.
int a = 0;
if(a == 1) {
int b = 0;
} //b scope ends here
if(a == 1) {
b = 1; //compiler error
}