Syntax errors in Java
See English answers > compiler error when declaring a variable inside if condition and no curry braces
if ( year % 4 == 0 ) int i = 0; else int j = 0;
The syntax error in eclipse due to this line of code is:
>Syntax error on token "int", delete this token > I can't solve the syntax error on variable > token "int", delete this token > J can't resolve to syntax error on variable > token "=", delete this token
I don't know why
From what I've observed, I think putting an int declaration in the if else constructor is implementing it
If I declare I and j earlier in the code and run the program, the error disappears as follows:
int i; int j; if ( year % 4 == 0 ) i = 0; else j = 0;
There are no grammatical errors in this case Why?
Solution
This may be because of the scope Declaring I and j in an IF statement makes them inaccessible outside the if statement Declaring them outside will change their scope