Why does the Java compiler complain about a local variable not having been initialized here?

Posted by pele on Stack Overflow See other posts from Stack Overflow or by pele
Published on 2010-03-02T09:05:28Z Indexed on 2010/04/24 6:33 UTC
Read the original article Hit count: 218

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about variable