The if
and the else
both have different scopes.
Variables declared in the if
can be used only in the if
and any of its children
Similarly, variables declared in the else
can be used only in the else
and any of its children
Read more about variable scope for more information.
solved Why does following Java program does not give error?