Sunday, September 27, 2009

Puzzle 53 – Solution

The problem that occurred was that even though the Constants class was recompiled the MyData class still continues to retain the old value of the constant Author. This happens because static final fields are in-lined into the code.

As c0dep0et points out in his comment,
"From JLS:
Simple names that refer to final variables whose initializers are constant expressions qualify to be compile-time constants."

One way to resolve this problem would be to make sure that all files depended on the Constants file are recompiled when the Constants file is recompiled. (Alternately not declaring the field as final would work!).

Modern IDE are more intelligent and usually recompile depended class when a final static field is changed in a class!

No comments:

Post a Comment

Solution for this question?