I have a Java project that involves the class GUIConstants–various public static final parameters used for laying out the GUI, since different components sometimes have to be the same size or color or whatever.
I’m currently in a stage of doing a visual redesign, which involves changing a few of those constants. However, ant is making this difficult. I’ll change a parameter and recompile, but the old value is still used. If I add some trivial modification to one of the files that uses it and recompile, the correct value will be used. But it’s annoying and error-prone to have to track down all the files and modify them. Surely there’s a way to force ant to recompile unchanged files…I just couldn’t find it in the man page.
Sidenote: My hypothesis is that when a class that uses a final variable is compiled, Java instead uses the value itself, not a reference to whatever the variable is pointing at (in a similar fashion to the way using #DEFINE for constants works in C). So even when the variable points to something else, the original value is baked into the .class file. Is this true? (It doesn’t affect my problem, I’m just curious.)
Thanks in advance.
Your hypothesis is almost right. It’s not simply
finalvariables though, but so called "constant variables":Then in the section on binary compatibility (§13.1):
and (§13.4.9) (my emphasis):
In a previous job, we made use of this to have a sort of conditional compilation system, so that we could produce production binaries with all the debug statements stripped out.
When you combine this with the way the
javactask determines what classes to recompile you get the behaviour you are seeing:The simplest way to fix this would be to do a clean full compile every time, with something like
ant clean compile(assuming you have acleantarget that removes all your class files). This might be too slow though.I was also going to suggest that you look at the
dependtask, as suggested in the docs for thejavactask, but looking at the docs for it (I haven’t actually used it myself) it seems that it won’t help (see the section titled "Limitations"):One possible workaround, if you find doing a clean compile every time too slow, would be to make the values in your
GUIConstantsclass not be constants, at least whilst you make your changes. You could make the values non-final by commenting out all thefinalkeywords, then the other classes should see your changes. When you are happy with the new values, put thefinals back in and recompile (and test that everything is still working properly of course).