{"id":4546,"date":"2014-03-30T13:27:50","date_gmt":"2014-03-30T13:27:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-final-field-compile-time-constant-expression-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:27:50","modified_gmt":"2014-03-30T13:27:50","slug":"java-final-field-compile-time-constant-expression-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-final-field-compile-time-constant-expression-collection-of-common-programming-errors\/","title":{"rendered":"Java final field compile-time constant expression-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p>I couldn&#8217;t understand the statement changes to the final field may not be observed<\/p>\n<\/blockquote>\n<p>It tells that , if a final variable is declared as compile time constant then any change made in the final variable using <strong>reflection API<\/strong> further in program will not be visible to the program during execution.<br \/>\nFor example consider the code given below:<\/p>\n<pre><code>import java.lang.reflect.*;\nclass ChangeFinal \n{\n    private final int x = 20;\/\/compile time constant\n    public static void change(ChangeFinal cf)\n    {\n        try\n        {\n            Class clazz = ChangeFinal.class;\n            Field field = clazz.getDeclaredField(\"x\");\n            field.setAccessible(true);\n            field.set(cf , 190);\/\/changed x to 190 for object cf\n        }\n        catch (Exception ex)\n        {\n            ex.printStackTrace();\n        }\n    }\n    public static void main(String[] args) \n    {\n        ChangeFinal cf = new ChangeFinal();\n        System.out.println(cf.x);\/\/prints 20\n        change(cf);\n        System.out.println(cf.x);\/\/prints 20\n    }\n}\n<\/code><\/pre>\n<p>The Output of the above code is:<\/p>\n<pre><code>20\n20\n<\/code><\/pre>\n<p><strong>WHY?<\/strong><br \/>\nThe answer lies in the output provided by <code>javap -c<\/code> command for public static void main:<\/p>\n<pre><code>public static void main(java.lang.String[]);\n  Code:\n   0:   new     #3; \/\/class ChangeFinal\n   3:   dup\n   4:   invokespecial   #11; \/\/Method \"\":()V\n   7:   astore_1\n   8:   getstatic       #12; \/\/Field java\/lang\/System.out:Ljava\/io\/PrintStream;\n   11:  aload_1\n   12:  invokevirtual   #13; \/\/Method java\/lang\/Object.getClass:()Ljava\/lang\/Cla\nss;\n   15:  pop\n   16:  bipush  20\n   18:  invokevirtual   #14; \/\/Method java\/io\/PrintStream.println:(I)V\n   21:  aload_1\n   22:  invokestatic    #15; \/\/Method change:(LChangeFinal;)V\n   25:  getstatic       #12; \/\/Field java\/lang\/System.out:Ljava\/io\/PrintStream;\n   28:  aload_1\n   29:  invokevirtual   #13; \/\/Method java\/lang\/Object.getClass:()Ljava\/lang\/Cla\nss;\n   32:  pop\n   33:  bipush  20\n   35:  invokevirtual   #14; \/\/Method java\/io\/PrintStream.println:(I)V\n   38:  return\n\n}\n<\/code><\/pre>\n<p>At line 16 (before <code>changeFinal<\/code> method is called)the value of <code>cf.x<\/code> is hardcoded to <code>20<\/code> . And at line 33 (after <code>changeFinal<\/code> method is called) the value of <code>cf.x<\/code> is again hardcoded to <code>20<\/code>. Therefore , Although the change in the value of final variable <code>x<\/code> is done successfully by <code>reflection API<\/code> during execution, but because of <code>x<\/code> being a compile time constant it is showing its constant value <code>20<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I couldn&#8217;t understand the statement changes to the final field may not be observed It tells that , if a final variable is declared as compile time constant then any change made in the final variable using reflection API further in program will not be visible to the program during execution. For example consider the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4546","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4546"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4546\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}