Why does the Java compiler complain about a local variable not having been initialized here?
- by pele
int a = 1, b;
if(a > 0) b = 1;
if(a <= 0) b = 2;
System.out.println(b);
If I run this, I receive:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable b may not have been initialized
at Broom.main(Broom.java:9)
I know that the local variables are not initialized and is your duty to do this, but in this case, the first if doesn't initialize the variable?