{"id":456,"date":"2008-11-22T09:26:52","date_gmt":"2008-11-22T09:26:52","guid":{"rendered":"http:\/\/www.gmarwaha.com\/blog\/?p=456"},"modified":"2014-09-14T05:18:12","modified_gmt":"2014-09-14T05:18:12","slug":"groovy-%e2%80%93-add-static-instance-methods-dynamically","status":"publish","type":"post","link":"https:\/\/www.gmarwaha.com\/blog\/2008\/11\/22\/groovy-%e2%80%93-add-static-instance-methods-dynamically\/","title":{"rendered":"Groovy \u2013 Add Static &#038; Instance methods dynamically"},"content":{"rendered":"<p>Like Ruby and other dynamic languages in its Genre, groovy also allows us to add methods to a class dynamically at runtime. You need not restrict yourself to adding instance methods alone. Even static methods are supported. <\/p>\n<p>To showcase this, let\u2019s create a class called User with name and email properties.<\/p>\n<pre class=\"prettyprint\">\r\nclass User {\r\n  String name, email\r\n}\r\n<\/pre>\n<p>Now, let\u2019s add a static newInstance() method to create a new instance of this class. Technically speaking this is not a method; it is a closure. This example is contrived, but I hope you get the idea.<\/p>\n<pre class=\"prettyprint\">\r\nUser.metaClass.static.newInstance = {name, email ->\r\n  new User(name: name, email: email)\r\n}\r\n<\/pre>\n<p>Now, let\u2019s add an instance method to update the \u201cname\u201d property of User. Here, the \u201cdelegate\u201d references the \u201cthis\u201d pointer. Remember, this is not a method; it is a closure.<\/p>\n<pre class=\"prettyprint\">\r\nUser.metaClass.updateName = { name ->\r\n  delegate.name = name\r\n}\r\n<\/pre>\n<p>The very next line you can start creating a new instance of user using this dynamically added static method and instance method as shown below.  <\/p>\n<pre class=\"prettyprint\">\r\nUser user = User.newInstance('ganesh', 'g@e.com')\r\nassert 'ganesh' == user.name\r\n\r\nuser.updateName (\"ganeshji\")\r\nassert \"ganeshji\" == user.name\r\n<\/pre>\n<p>It is not necessary that you add these new methods right below the class. You can add it anywhere in your code. As long as you call these methods after the lines that define these new methods are executed you should be fine. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like Ruby and other dynamic languages in its Genre, groovy also allows us to add methods to a class dynamically at runtime. You need not restrict yourself to adding instance methods alone. Even static methods are supported. To showcase this, let\u2019s create a class called User with name and email properties. class User { String name, email } Now, let\u2019s add a static newInstance() method to create a new instance of this class. Technically speaking this is not a method; it is a closure. This example is contrived, but I hope you get the idea. User.metaClass.static.newInstance = {name, email ->... <br \/><a class=\"moretag\" href=\"https:\/\/www.gmarwaha.com\/blog\/2008\/11\/22\/groovy-%e2%80%93-add-static-instance-methods-dynamically\/\">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-456","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\/456","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=456"}],"version-history":[{"count":10,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"predecessor-version":[{"id":814,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions\/814"}],"wp:attachment":[{"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/media?parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gmarwaha.com\/blog\/wp-json\/wp\/v2\/tags?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}