This is what my method looks like
public int abc()
{
int x;
if(x > 100)
{
//Say ok
return x;//Causes compiler error
}
//if something more, x = some number
return x;//Causes compiler error
}
I saw an answer at SO - Java: "Local variable may not have been initialized" not intelligent enough?
But, I am still not sure why this error happens. Its a compiler error and not a warning. So, the problem must be something more serious than "taking a safe route".
Local variables must be initialized before they are accessed. This is why there is a compiler error.
You should must have an error at the if
statement where it's first accessed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.