# HG changeset patch # User Jaroslav Tulach # Date 1224226868 -7200 # Node ID f6369ddf822ca223451de417d42c86ab0225c98b # Parent b762c8fb572e3c75fb8e42047e149315784f034a ljnelson noted at 'http://weblogs.java.net/blog/jst/archive/2008/10/the_better_comp.html' that it is enough to make the variable final and the problem goes away. True, final helps, however the same code without final works as well. This very likely means that the compiler puts the variable into the topmost block where it is guaranteed to be fully initialized. That is why we need a hint warn about declaration of non-fully initialized variables. diff -r b762c8fb572e -r f6369ddf822c samples/gc/test/org/apidesign/gc/CompilerSurprisesTest.java --- a/samples/gc/test/org/apidesign/gc/CompilerSurprisesTest.java Wed Oct 15 21:48:18 2008 +0200 +++ b/samples/gc/test/org/apidesign/gc/CompilerSurprisesTest.java Fri Oct 17 09:01:08 2008 +0200 @@ -61,6 +61,31 @@ } // END: compiler.surprises.fix +// BEGIN: compiler.surprises.fix.init + @Test public void properInitializationFixesTheProblem() { + String retValue; + if (yes) { + retValue = factory(); + } else { + retValue = null; + } + assertNotGC("Cannot be GCed, now the retValue is on stack", cache); + } +// END: compiler.surprises.fix.init + +// BEGIN: compiler.surprises.fix.final + @Test public void properUsingFinalFixesTheProblem() { + final String retValue; + if (yes) { + retValue = factory(); + } else { + retValue = null; + } + assertNotGC("Cannot be GCed, now the retValue is on stack", cache); + } +// END: compiler.surprises.fix.final + + private static void assertGC(String msg, Reference ref) { NbTestCase.assertGC(msg, ref); }