{"id":132,"date":"2014-07-16T16:03:45","date_gmt":"2014-07-16T10:33:45","guid":{"rendered":"http:\/\/codeforgeek.com\/?p=132"},"modified":"2021-06-18T08:13:48","modified_gmt":"2021-06-18T02:43:48","slug":"power-keyword-java","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/power-keyword-java\/","title":{"rendered":"Power of this keyword in Java"},"content":{"rendered":"<p><strong><em>this<\/em> <\/strong>keyword is a reference ( which means it manages some relation ) to the current object where it is called ( can be constructor,function etc). It helps to keep the control of scope to access variables or any other elements. <em>this\u00a0<\/em>keyword is very powerful and widely used in complex Java application.<\/p>\n<blockquote><p>NOTE: All code is from Official documentation of Oracle Java.<\/p><\/blockquote>\n<p>for example, refer this piece of Java code.<\/p>\n<p><code lang=\"java\"><br \/>\npublic class Point {<br \/>\npublic int x = 0;<br \/>\npublic int y = 0;<\/p>\n<p>\/\/constructor<br \/>\npublic Point(int a, int b) {<br \/>\nx = a;<br \/>\ny = b;<br \/>\n}<br \/>\n}<br \/>\n<\/code><br \/>\nso when we create object of &#8220;<strong>Point<\/strong>&#8221; class, constructor will assign the parameter values &#8220;a&#8221; and &#8220;b&#8221; to &#8220;x&#8221; and &#8220;y&#8221; declared globally in class. Pretty straight. Now take a look at\u00a0this code.<\/p>\n<p><code lang=\"java\"><br \/>\npublic class Point {<br \/>\npublic int x = 0;<br \/>\npublic int y = 0;<\/p>\n<p>\/\/constructor<br \/>\npublic Point(int x, int y) {<br \/>\nthis.x = x;<br \/>\nthis.y = y;<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Here we are using same name of parameters in constructor as well as global declared variables. Now let&#8217;s create the object of this class.<\/p>\n<blockquote><p>Point ob=new Point(10,20);<\/p><\/blockquote>\n<p>Here, 10 and 20 will be assigned to &#8220;<strong>x<\/strong>&#8221; and &#8220;<strong>y<\/strong>&#8221; of Point constructor and then again assigned to <strong>this.x<\/strong> (x declared in class globally) and <strong>this.y<\/strong>. That means we have differentiated the variable, whether it is from function call or defined in class globally using &#8220;<em><strong>this<\/strong><\/em>&#8221; keyword.<\/p>\n<h3>Can we call constructor from constructor ? Yes<\/h3>\n<p>You can use &#8220;<em><strong>this<\/strong><\/em>&#8221; keyword to call constructor from one constructor depending upon your need. Here is simple code to show how to do so. This is official doc example of Oracle.<\/p>\n<p><code lang=\"java\"><br \/>\npublic class Rectangle {<br \/>\nprivate int x, y;<br \/>\nprivate int width, height;<\/p>\n<p>public Rectangle() {<br \/>\nthis(0, 0, 1, 1);<br \/>\n}<br \/>\npublic Rectangle(int width, int height) {<br \/>\nthis(0, 0, width, height);<br \/>\n}<br \/>\npublic Rectangle(int x, int y, int width, int height) {<br \/>\nthis.x = x;<br \/>\nthis.y = y;<br \/>\nthis.width = width;<br \/>\nthis.height = height;<br \/>\n}<br \/>\npublic static void main(String args[])throws Exception<br \/>\n{<br \/>\nRectangle r1=new Rectangle();<br \/>\nRectangle r2=new Rectangle(10,20);<br \/>\nRectangle r3=new Rectangle(1,2,10,20);<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Upon creation of Object &#8220;<strong>r1<\/strong>&#8221; , it will initiate the default constructor and from that &#8220;<strong>default<\/strong>&#8221; constructor will call another constructor with same name but with 4 parameters. Hope you understand the importance of &#8220;<em><strong>this<\/strong><\/em>&#8221; in java. If you have any doubt, ask me in comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>this keyword is a reference ( which means it manages some relation ) to the current object where it is called ( can be constructor,function etc). It helps to keep the control of scope to access variables or any other elements. this\u00a0keyword is very powerful and widely used in complex Java application. NOTE: All code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":137,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[8],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner.png",688,400,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner-300x174.png",300,174,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner.png",688,400,false],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner.png",688,400,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner.png",688,400,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2014\/07\/banner.png",688,400,false]},"uagb_author_info":{"display_name":"Shahid","author_link":"https:\/\/codeforgeek.com\/author\/shahid\/"},"uagb_comment_info":0,"uagb_excerpt":"this keyword is a reference ( which means it manages some relation ) to the current object where it is called ( can be constructor,function etc). It helps to keep the control of scope to access variables or any other elements. this\u00a0keyword is very powerful and widely used in complex Java application. NOTE: All code&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/137"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}