{"id":165,"date":"2010-03-12T05:20:01","date_gmt":"2010-03-12T05:20:01","guid":{"rendered":"http:\/\/www.gmarwaha.com\/blog\/?p=165"},"modified":"2014-09-14T04:49:10","modified_gmt":"2014-09-14T04:49:10","slug":"groovy-no-arg-constructor-is-always-there","status":"publish","type":"post","link":"https:\/\/www.gmarwaha.com\/blog\/2010\/03\/12\/groovy-no-arg-constructor-is-always-there\/","title":{"rendered":"Groovy: No-arg constructor is always there"},"content":{"rendered":"<p>Do you usually write a no-arg constructor for your Java classes? You don&#8217;t have to &#8211; unless you need to initialize something in that constructor. Behind the scenes, the Java compiler adds the no-arg constructor directly into the byte-code if none is provided. But when one or more overloaded constructors are provided for that class, the compiler doesn&#8217;t add the default no-arg constructor to the byte-code. This has been part of Java since the beginning. <\/p>\n<p>Fast forward to today. I was doing some work with Groovy. All of a sudden, i realized that i forgot to add a no-arg constructor inspite of adding multiple constructors to a class and the code was still working without errors. That is when I found out that the Groovy compiler adds the no-arg constructor to the byte-code regardless of other overloaded constructors. I just thought I should share this learning with the world and hence this blog!<\/p>\n<p>Does JRuby, Jython, Scala and other JVM languages do it too? Will have to check&#8230;<\/p>\n<p>Don&#8217;t get me started on why i should use overloaded constructors when Groovy provides dynamic constructors out of the box. The short answer is &#8220;API Requirement&#8221; from the client. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you usually write a no-arg constructor for your Java classes? You don&#8217;t have to &#8211; unless you need to initialize something in that constructor. Behind the scenes, the Java compiler adds the no-arg constructor directly into the byte-code if none is provided. But when one or more overloaded constructors are provided for that class, the compiler doesn&#8217;t add the default no-arg constructor to the byte-code. This has been part of Java since the beginning. Fast forward to today. I was doing some work with Groovy. All of a sudden, i realized that i forgot to add a no-arg constructor... <br \/><a class=\"moretag\" href=\"https:\/\/www.gmarwaha.com\/blog\/2010\/03\/12\/groovy-no-arg-constructor-is-always-there\/\">Continue reading...<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,15],"tags":[],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-groovy","category-modern-languages"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":10,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":804,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts\/165\/revisions\/804"}],"wp:attachment":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}