Moral of the story in all this: use clear and unambiguous initialization patterns.
Alexey ________________________________ From: Tom Hawtin <[email protected]> To: The Java Posse <[email protected]> Sent: Tue, November 23, 2010 5:35:23 AM Subject: [The Java Posse] Re: Inner Class Conundrum On Nov 22, 4:53 pm, Craig Kelley <[email protected]> wrote: > We have an application targeting jre6 that is getting null pointer > exceptions from variables supposedly declared final, referenced from > inner classes. I am guess the difference is in the javac compilation, and which JRE version you are targeting. `javap -c` is your friend. >From memory, javac in jdk1.4 defaulted to generating code for earlier releases. jdk5 and 6 uses the current version. The important thing is that we are comparing pre-1.4 code with post-1.4 code. If you use code compiled with 1.4 on 6, you should see the 1.4 behaviour. It used to be if you used -source 1.3 -target 1.3 on jdk6 you would see the 1.4 results, but I think that may have been dropped quietly at some point. What's happening in 1.4? The base class constructor is executed by the derived class' implicit super() call. You are not allowed to change fields before calling super, so locals from the outer scope cannot have been copied in yet (this is how java 1.1 implements closure). In 1.4, the JVM spec was relaxed to allow the context to be copied into the inner class object before calling the constructor. This is only permitted on jre 1.4 and later. With -target 1.4 or later, construction of inner classes will use this to be able to set fields before calling the constructor. The old behaviour is a particular problem with factory methods (if you like that GoF pattern). Tom -- 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. -- 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.
