I tried to come up with obscure test cases for an alternative open source JVM that I help with ( Avian ) when I came across an interesting bit of code, and I was surprised that it did not compile:
public class Test { public static int test1() { int a; try { a = 1; return a;
The most obvious code path (the only one I see) is to execute a = 1, "try" to return (first time), and then execute finally, which actually returns a. However, javac complains that "a" may not have been initialized:
Test.java:8: variable a might not have been initialized
return a;
^
The only thing I can think of that can cause / resolve another code path is that after the start of the attempt, an incomprehensible exception occurred at runtime, but before the value 1 is set to a-something similar to OutOfMemoryError or a StackOverflowException exception, but I can't think of a single case where this could happen at this point in the code.
Could someone more familiar with the specifics of the Java standard shed some light on this? Is it just a case where the compiler is conservative and therefore refuses to compile what would otherwise be the correct code, or is something weirder going on here?
java variables initialization finally
Joshua warner
source share