{"id":942,"date":"2014-03-12T07:21:18","date_gmt":"2014-03-12T14:21:18","guid":{"rendered":"http:\/\/10kdev.net\/?p=942"},"modified":"2014-03-12T09:23:23","modified_gmt":"2014-03-12T16:23:23","slug":"final-variables-in-groovy-curiosity","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=942","title":{"rendered":"Final Variables in Groovy Curiosity"},"content":{"rendered":"<p id=\"yui_3_13_0_ym1_1_1394632282564_2723\">(On my local I&#8217;m running Groovy 2.1.9 on Java\/JDK 6).<\/p>\n<p id=\"yui_3_13_0_ym1_7_1394632282564_67\">Was having a Discussion of <em><strong>final<\/strong> <\/em>and <em><strong>static<\/strong><\/em>, and decided to try something in groovyConsole.\u00a0 In the Java world, a final variable cannot be reassigned a value once initialized.<\/p>\n<p>Groovy Code:<\/p>\n<p id=\"yui_3_13_0_ym1_1_1394632282564_2731\" style=\"padding-left: 30px;\">final String i = &#8220;test&#8221;<br \/>\nprintln i<br \/>\ni = &#8220;red&#8221;<br id=\"yui_3_13_0_ym1_7_1394632282564_24\" \/>println i<\/p>\n<p id=\"yui_3_13_0_ym1_7_1394632282564_25\">Output:<\/p>\n<p id=\"yui_3_13_0_ym1_7_1394632282564_32\" style=\"padding-left: 30px;\">groovy&gt; final String i = &#8220;test&#8221;<br \/>\ngroovy&gt; println i<br \/>\ngroovy&gt; i = &#8220;red&#8221;<br \/>\ngroovy&gt; println i<\/p>\n<p style=\"padding-left: 30px;\">test<br id=\"yui_3_13_0_ym1_7_1394632282564_51\" \/>red<\/p>\n<p id=\"yui_3_13_0_ym1_7_1394632282564_48\">\u00a0Interesting &#8212; Groovy completely ignored <em><strong>final<\/strong><\/em>.\u00a0 If I plug the code into a Java compiler I get a compilation error &#8212; can&#8217;t reassign.<\/p>\n<p>I sent this off to a buddy of mine who teaches Groovy to corporate and he found it odd, and said this behavior happens for this example as well:<\/p>\n<p>Groovy Code:<\/p>\n<p style=\"padding-left: 30px;\">def junk() {<br \/>\nfinal int a = 42;<br \/>\na = 99;<br \/>\n}<\/p>\n<p style=\"padding-left: 30px;\">println junk()<\/p>\n<p>Output:<\/p>\n<p style=\"padding-left: 30px;\">groovy&gt; def junk() {<br \/>\ngroovy&gt; final int a = 42;<br \/>\ngroovy&gt; a = 99;<br \/>\ngroovy&gt; }<br \/>\ngroovy&gt; print junk()<\/p>\n<p style=\"padding-left: 30px;\">99<\/p>\n<p id=\"yui_3_13_0_ym1_7_1394632282564_52\">The discussion is a little extended, found this though &#8212; the issue for final is still open as of this date (March 12, 2014):<\/p>\n<p>http:\/\/jira.codehaus.org\/browse\/GROOVY-4681<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(On my local I&#8217;m running Groovy 2.1.9 on Java\/JDK 6). Was having a Discussion of final and static, and decided to try something in groovyConsole.\u00a0 In the Java world, a final variable cannot be reassigned a value once initialized. Groovy Code: final String i = &#8220;test&#8221; println i i = &#8220;red&#8221;println i Output: groovy&gt; final [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23,24],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/942"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=942"}],"version-history":[{"count":6,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/942\/revisions"}],"predecessor-version":[{"id":948,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/942\/revisions\/948"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=942"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}