Static Final String Constants

10 September 2004

Mr Brunning found the inlining issue with static finals recently. I saw the exact same issue, and went through many of the same steps. Our application is exploded in the filesystem, so we hoped to be able to change one class and not have to redploy the whole thing. This interesting side effect caused us the same headaches of having to redeploy anything that may have used our Constants class.

  1. Taking "final" off keeps it from happening, but also doesn't protect it from accidental changes.
  2. Using new String("principal") would have avoided the actual value being inlined, since it's more dynamic.
  3. Externalizing the strings in the Constants object into a properties file can fix this as well, and allows reconfiguration without recompilation.

This is an odd case where the compiler inlines the actual value instead of the normal reference to the object. I'm not sure ant should have been expected to recognize that to know to recompile -- Everything else is dynamically linked at runtime, so it normally wouldn't have required a recompile.


Filed Under: Java