Why this doesn't want compile, if in other condition like if(8>0) will
public class StartClass {
public static void main(String[] args) {
int i;
boolean b = true;
if (b) {
i = 1;
}
System.out.println(i);//error
}
}
Why this doesn't want compile, if in other condition like if(8>0) will
public class StartClass {
public static void main(String[] args) {
int i;
boolean b = true;
if (b) {
i = 1;
}
System.out.println(i);//error
}
}
The answer is:
java doesn't give initial value to variable declared inside methods..
int i
is declared inside the main method, so there is no default initial value for that...
if b never gets to be true then i never gets initialized,
So the compiler complains because it may happen that i has no value
ergo: you are not going to be able to do System.out.println(i);