The thing that baffles me is not the 'null' - that's how java works. When an object is created, state is built from java.lang.Object up through all parents, and then finally, at the end, the class you instantiated gets a shot at initializing. But, virtual lookup works right away, and thus fields can have their uninitialized values, even final ones.
The thing I don't get is why in jre 6 you're getting 'hello' twice. I'd ask what happens when String s isn't final, but to test that you need to do some serious rewriting. I know some changes were added in jre 5 to make sure final fields cant take on different values due to multi-threading issues, which might have something to do with this. Alternatively, some change was made in how variables from outer scope are transferred to the inner class. Normally this is done via an invisible synthetic constructor. As far as I know this still happens (which is where the initialization order issues come from), but maybe this was changed. On Nov 22, 5:53 pm, Craig Kelley <[email protected]> wrote: > Hello Java Posse. > > In a break from Oracle and Google legal fun, I thought I'd throw this > problem out there. We're running into a strange, what seems to be JVM- > related issue. Given the following classes: > > abstract class Inner { > Inner() { run(); } > public abstract void run(); > > } > > public class Outer { > public static void test(final String s) { > Inner i = new Inner() { > public void run() { > System.out.println("s = " + s); > } > }; > i.run(); > } > > public static void main(String[] args) { > test("hello"); > } > > } > > Why does jre 1.4 return this: > > s = null > s = hello > > But jre 6 returns this: > > s = hello > s = hello > > We have an application targeting jre6 that is getting null pointer > exceptions from variables supposedly declared final, referenced from > inner classes. -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
