{"id":11688,"date":"2012-07-12T11:36:00","date_gmt":"2012-07-12T10:36:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=de0724f5f065e8eaf23f7b23427631f5"},"modified":"2022-07-28T12:29:41","modified_gmt":"2022-07-28T10:29:41","slug":"private-fields-and-methods-are-not-private-in-groovy","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2012\/07\/12\/private-fields-and-methods-are-not-private-in-groovy\/","title":{"rendered":"Private fields and methods are not private in groovy"},"content":{"rendered":"<p>I used to code in Java before I met groovy. Like most of you, groovy attracted me with many enhancements. This was to my surprise to discover that method visibility in groovy is handled different than Java!<\/p>\n<p>Consider this example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">\nclass Person {\nprivate String name\npublic String surname\n\nprivate Person() {}\n\nprivate String signature() { \"${name?.substring(0, 1)}. $surname\" }\n\npublic String toString() { \"I am $name $surname\" }\n}\n<\/pre>\n<p>How is this class interpreted with Java?<\/p>\n<ol>\n<li>Person has private constructor that cannot be accessed<\/li>\n<li>Field &#8220;name&#8221; is private and cannot be accessed<\/li>\n<li>Method signature() is private and cannot be accessed<\/li>\n<\/ol>\n<p>Let&#8217;s see how groovy interpretes Person:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">\npublic static void main(String[] args) {\ndef person = new Person() \/\/ constructor is private - compilation error in Java\nprintln(person.toString())\n\nperson.@name = 'Mike' \/\/ access name field directly - compilation error in Java\nprintln(person.toString())\n\nperson.name = 'John' \/\/ there is a setter generated by groovy\nprintln(person.toString())\n\nperson.@surname = 'Foo' \/\/ access surname field directly\nprintln(person.toString())\n\nperson.surname = 'Bar' \/\/ access auto-generated setter\nprintln(person.toString())\n\nprintln(person.signature()) \/\/ call private method - compilation error in Java\n}\n<\/pre>\n<p>I was really astonished by its output:<\/p>\n<pre class=\"EnlighterJSRAW\">\nI am null null\nI am Mike null\nI am John null\nI am John Foo\nI am John Bar\nJ. Bar\n<\/pre>\n<p>As you can see, groovy does not follow visibility directives at all! It treats them as non-existing. Code compiles and executes fine. It&#8217;s contrary to Java. In Java this code has several errors, pointed out in comments.<\/p>\n<p>I&#8217;ve searched a bit on this topic and it seems that this behaviour is known since version 1.1 and there is a bug report on that: <a href=\"http:\/\/jira.codehaus.org\/browse\/GROOVY-1875\">http:\/\/jira.codehaus.org\/browse\/GROOVY-1875<\/a>. It is not resolved even with groovy 2 release. As Tim Yates mentioned in <a href=\"http:\/\/stackoverflow.com\/questions\/7852370\/private-method-groovy-is-not-private\">this Stackoverflow question<\/a>: &#8220;<em>It&#8217;s not clear if it is a bug or by design<\/em>&#8220;. Groovy treats visibility keywords as a hint for a programmer.<\/p>\n<p>I need to keep that lesson in mind next time I want to make some field or method private!<\/p>\n","protected":false},"excerpt":{"rendered":"I used to code in Java before I met groovy. Like most of you, groovy attracted me with many enhancements. This was to my surprise to discover that method visibility in groovy is handled different than Java!\nConsider this example:\nclass Person {private String namepublic  String surnameprivate Person() {}private String signature() { \"${name?.substring(0, 1)}. $surname\" }public String toString() { \"I am $name $surname\" }}\nHow is this class interpreted with Java?\n\nPerson has private constructor that cannot be accessed\nField &#8220;name&#8221; is private and cannot be accessed\nMethod signature() is private and cannot be accessed\n\nLet&#8217;s see how groovy interpretes Person:\npublic static void main(String[] args) {def person = new Person()   \/\/ constructor is private - compilation error in Javaprintln(person.toString())    person.@name = 'Mike'       \/\/ access name field directly - compilation error in Javaprintln(person.toString())    person.name = 'John'        \/\/ there is a setter generated by groovyprintln(person.toString())    person.@surname = 'Foo'     \/\/ access surname field directlyprintln(person.toString())    person.surname = 'Bar'      \/\/ access auto-generated setterprintln(person.toString())println(person.signature()) \/\/ call private method - compilation error in Java}\nI was really astonished by its output:\nI am null nullI am Mike nullI am John nullI am John FooI am John BarJ. Bar\nAs you can see, groovy does not follow visibility directives at all! It treats them as non-existing. Code compiles and executes fine. It&#8217;s contrary to Java. In Java this code has several errors, pointed out in comments.\nI&#8217;ve searched a bit on this topic and it seems that this behaviour is known since version 1.1 and there is a bug report on that: http:\/\/jira.codehaus.org\/browse\/GROOVY-1875. It is not resolved even with groovy 2 release. As Tim Yates mentioned in this Stackoverflow question: &#8220;It&#8217;s not clear if it is a bug or by design&#8220;. Groovy treats visibility keywords as a hint for a programmer.\nI need to keep that lesson in mind next time I want to make some field or method private!\n","protected":false},"author":37,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[50],"class_list":{"0":"post-11688","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-groovy"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=11688"}],"version-history":[{"count":3,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11688\/revisions"}],"predecessor-version":[{"id":14642,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11688\/revisions\/14642"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=11688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=11688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=11688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}