{"id":128,"date":"2004-04-21T00:38:19","date_gmt":"2004-04-21T06:38:19","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=128"},"modified":"2004-04-21T00:38:19","modified_gmt":"2004-04-21T06:38:19","slug":"inlining-of-final-variables-and-recompilation","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/128","title":{"rendered":"Inlining of final variables and recompilation"},"content":{"rendered":"<p>This problem that has bitten me in the ass a few times, and I&#8217;d love to hear any bright ideas on how y&#8217;all avoid it.<\/p>\n<p>Suppose you have an interface that defines some useful constants:<\/p>\n<p><code>public interface foo {<br \/>\n&nbsp;int five = 6;<br \/>\n}<\/code><\/p>\n<p>and a class that uses those constants:<\/p>\n<p><code>public class bar {<br \/>\n&nbsp;public static void main(String[]args) {<br \/>\n&nbsp;&nbsp;System.out.println(\"five: \"+foo.five);<br \/>\n&nbsp;}<br \/>\n}<\/code><\/p>\n<p>All well and good, until you realize that five isn&#8217;t really 6, it&#8217;s 5.  Whoops, change the <code>foo<\/code> java file and rebuild, right?  Well, if you use <code>javac *.java<\/code> to do this (as you might, if you only have the <code>foo<\/code> and <code>bar<\/code> files), then you&#8217;ll be alright.  <\/p>\n<p>But, if you&#8217;re like the other 99% of the java development world, and you use a build tool, like <a href='http:\/\/ant.apache.org'>ant<\/a>, smart enough to look at timestamps, you&#8217;ll still get 6 for the output of <code>java bar<\/code>. Ant is smart enough to look at the timestamps of .class and .java files to determine which .java files have changed since it last did a compilation.  But it is too dumb to realize that the <code>bar<\/code> class has  a dependency on <code>foo<\/code>, and should thus be recompiled even though <code>bar.java<\/code> is older than <code>bar.class<\/code>.  (I haven&#8217;t looked at the byte code, but I expect that the value of <code>five<\/code> is just inlined into the <code>bar<\/code> class because it&#8217;s a final variable.)  If you&#8217;re using a make based build system, I believe you can use <a href='http:\/\/www.vet.com.au\/java\/javadeps\/'>javadeps<\/a> to build out the correct dependency list, but I haven&#8217;t seen anything similar for ant.  Another options is to just remember to blow away your build directory anytime you change your &#8216;constants&#8217;.<\/p>\n<p>I guess this is why <a href='http:\/\/javaalmanac.com\/egs\/java.util\/Props.html'>properties files<\/a> might be a better choice for this type of configuration information, because they&#8217;re always read in anew at startup, and thus cannot be inlined (since they&#8217;re a runtime thing).  Of course, then you lose the benefits of type checking.  Not sure what the correct answer is.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This problem that has bitten me in the ass a few times, and I&#8217;d love to hear any bright ideas on how y&#8217;all avoid it. Suppose you have an interface [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}