tag:blogger.com,1999:blog-9193160645817040317.post5018079246494177271..comments2015-12-22T12:31:53.234+05:30Comments on Twisters - The New Age Java Quiz: Puzzle 53 – Statically Speaking.Saifuddin Merchanthttp://www.blogger.com/profile/09008041357659535766noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-9193160645817040317.post-40017065086417900472009-09-26T11:32:30.172+05:302009-09-26T11:32:30.172+05:30This is got to be something with static keyword. I...This is got to be something with static keyword. I also dont have the complete idea about , what happens when we declare a variable as static . Luking forward to hear a good answer from you.<br /><br />Thanks.vishwanathhttps://www.blogger.com/profile/03753309726590344870noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-47620582183726371782009-09-24T20:04:23.516+05:302009-09-24T20:04:23.516+05:30The class variable AUTHOR in Constants is a final ...The class variable AUTHOR in Constants is a final variable making it a compile time constant. <br />From JLS:<br />'Simple names that refer to final variables whose initializers are constant expressions' (qualify to be compile-time constants)<br /><br />References to compile time constants are resolved at compile-time to a copy of the compile-time constant value and hence accessing these fields does not cause an initialization of the class containing the field.<br /><br />The easiest fix here would be to make AUTHOR a non-final variable.<br /><br />class Constants{<br /> public static String AUTHOR = "ANEESH"; <br />}<br /><br />or keep the final keyword , make it non-static and initialize it in the Constructor<br /><br />class Constants{<br /> public final String AUTHOR;<br /> <br /> public Constants(){<br /> AUTHOR="ANEESH";<br /> }<br />}<br /><br />That requires more changes than the first one of-course.Anonymoushttps://www.blogger.com/profile/13025131309387702914noreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-10044012815698080932009-09-22T17:18:03.623+05:302009-09-22T17:18:03.623+05:30static final fields are not referenced but are inl...static final fields are not referenced but are inlined into the code.<br /><br />your friend in this case only compile 1 part of the code which will not replace the text where it is inlined.<br /><br />sollution:<br /><br />1) re-compile the complete code.<br />2) don't use static final :)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9193160645817040317.post-45123196230507880532009-09-20T16:30:23.790+05:302009-09-20T16:30:23.790+05:30Constants are inlined by compiler. So old AUTHOR v...Constants are inlined by compiler. So old AUTHOR value used in MyDate class even after Constants was recompiled.TheMalkolmhttps://www.blogger.com/profile/11358745568436186926noreply@blogger.com