{"id":5268,"date":"2021-03-23T16:28:30","date_gmt":"2021-03-23T16:28:30","guid":{"rendered":"https:\/\/ded9.com\/?p=5268"},"modified":"2026-02-18T11:47:56","modified_gmt":"2026-02-18T11:47:56","slug":"learn-this-keyword-in-java-in-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/learn-this-keyword-in-java-in-simple-language\/","title":{"rendered":"Learn the \u201cthis\u201d Keyword in Java \u2014 Simple Explanation with Practical Examples"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will learn the keyword this in <a href=\"https:\/\/ded9.com\/how-to-start-learning-java\/\">Java<\/a>. An example will help you learn how and where to use it.<\/span><span id=\"more-50136\"><\/span><\/p>\n<h2>This keyword<\/h2>\n<p>In Java, this refers to the current object in methods or constructs.\u00a0Let&#8217;s give an example to prove it.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class MyClass {<\/li>\n<li dir=\"ltr\">int instVar;<\/li>\n<li dir=\"ltr\">MyClass (int instVar) {<\/li>\n<li dir=\"ltr\">this.instVar = instVar;<\/li>\n<li dir=\"ltr\">System.out.println (\u201cthis reference =\u201d + this);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">MyClass obj = new MyClass (8);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cobject reference =\u201d + obj);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">this reference = com.ThisAndThat.MyClass@74a14482<\/p>\n<p dir=\"ltr\">object reference = com.ThisAndThat.MyClass@74a14482<\/p>\n<\/blockquote>\n<p>Note that the obj and this object identifiers are the same. This amounts to referring to the current object.<\/p>\n<p>There are three situations in which this keyword is commonly used.<\/p>\n<h3><span style=\"font-size: 14pt;\">1. Use this to separate different variable sources<\/span><\/h3>\n<p>In Java, you cannot define two or more variables with the same name in a domain (class domain or method domain). However, the sample variables and parameters may have the same name. As:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">class MyClass {\r\n\r\nint instVar; \/\/ instVar instance variable\r\n\r\nMyClass (int instVar) {\/\/ instVar parameter\r\n\r\ninstVar = instVar;\r\n\r\n}\r\n\r\n}<\/pre>\n<\/div>\n<p>The Java compiler is mistaken in the above program due to the ambiguous names. Therefore, to solve this problem, the keyword this is used.<\/p>\n<p>Let&#8217;s look at an example without using this keyword:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class MyClass {<\/li>\n<li dir=\"ltr\">int instVar;<\/li>\n<li dir=\"ltr\">MyClass (int instVar) {<\/li>\n<li dir=\"ltr\">instVar = instVar;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">MyClass mc = new MyClass (8);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cmc.instVar =\u201d + mc.instVar);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">mc.instVar = 0<\/p>\n<\/blockquote>\n<p>You might expect the output to be 8. Still, instead, it printed zero because the Java compiler made a mistake due to the name ambiguity between the sample variable and the constructor parameter.<\/p>\n<p>Let&#8217;s rewrite the above code and use this keyword to solve this problem.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class MyClass {<\/li>\n<li dir=\"ltr\">int instVar;<\/li>\n<li dir=\"ltr\">MyClass (int instVar) {<\/li>\n<li dir=\"ltr\">this.instVar = instVar;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">MyClass obj = new MyClass (8);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cobj.instVar =\u201d + obj.instVar);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">mc.instVar = 8<\/p>\n<\/blockquote>\n<p>You will now get the expected output. When you create an object, the Java compiler knows which object the constructor used.<\/p>\n<p>When the Java compiler calls the constructor, the object that called the constructor replaces it inside the constructor.<\/p>\n<p>Note: If you submit a parameter with a different name than the sample variables, the compiler will automatically attach this keyword.<\/p>\n<p>This code:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">class MyClass {\r\n\r\nint instVar;\r\n\r\nMyClass (int i) {\r\n\r\ninstVar = i;\r\n\r\n}\r\n\r\n}<\/pre>\n<\/div>\n<p>It is equal to:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">class MyClass {\r\n\r\nint instVar;\r\n\r\nMyClass (int i) {\r\n\r\nthis.instVar = i;\r\n\r\n}\r\n\r\n}<\/pre>\n<\/div>\n<p>Another everyday use of this keyword is in a class&#8217;s getter and setter methods. For example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class POJO {<\/li>\n<li dir=\"ltr\">String name;<\/li>\n<li dir=\"ltr\">void setName (String name) {<\/li>\n<li dir=\"ltr\">this.name = name;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">String getName () {<\/li>\n<li dir=\"ltr\">return this.name;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">POJO pojo = new POJO ();<\/li>\n<li dir=\"ltr\">pojo.setName (&#8220;Toshiba&#8221;);<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;pojo.name:&#8221; + pojo.getName ());<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">pojo.name: Toshiba<\/p>\n<\/blockquote>\n<h3>2- Using this in overloading manufacturers<\/h3>\n<p>When working with builders, overloading, calling one builder over another, may be more useful. However, the creators cannot be explicitly identified. Therefore, you can use another form of this keyword to achieve this goal. (() this)<\/p>\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pseudocode\" target=\"_blank\" rel=\"noopener\">pseudo-code<\/a> is shown here:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">this (arg-list)<\/pre>\n<\/div>\n<p>Here&#8217;s how to use this in the manufacturer&#8217;s call.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Complex {<\/li>\n<li dir=\"ltr\">private int a, b;<\/li>\n<li dir=\"ltr\">\/\/ parameterize constructor<\/li>\n<li dir=\"ltr\">private Complex (int i, int j) {<\/li>\n<li dir=\"ltr\">this.a = i;<\/li>\n<li dir=\"ltr\">this.b = j;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">private Complex (int i) {<\/li>\n<li dir=\"ltr\">this (i, i);\u00a0\/\/ invokes Complex (int i, int j);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">private Complex () {<\/li>\n<li dir=\"ltr\">this (0);\u00a0\/\/ invokes Complex (int i);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">@Override<\/li>\n<li dir=\"ltr\">public String toString () {<\/li>\n<li dir=\"ltr\">return this.a + \u201d+\u201d + this.b + \u201ci\u201d;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Complex c1 = new Complex (2, 3);<\/li>\n<li dir=\"ltr\">Complex c2 = new Complex (3);<\/li>\n<li dir=\"ltr\">Complex c3 = new Complex ();<\/li>\n<li dir=\"ltr\">System.out.println (c1);<\/li>\n<li dir=\"ltr\">System.out.println (c2);<\/li>\n<li dir=\"ltr\">System.out.println (c3);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h4>Output<\/h4>\n<blockquote>\n<p dir=\"ltr\">2 + 3i<\/p>\n<p dir=\"ltr\">3 + 3i<\/p>\n<p dir=\"ltr\">0 + 0i<\/p>\n<\/blockquote>\n<p>In the above program, it does not matter which constructor is called when introducing the object; finally, the parametric constructor is called.<\/p>\n<p>Be careful when using this (). Constructors calling this () run slowly because calling an additional constructor adds another overhead. If your class is only used to create a handful of objects, using this () is useful. Another great advantage of using this () is reducing duplicate code.<\/p>\n<h3>3- Send this as an argument<\/h3>\n<p>You can use this if you need to move the current object as an argument to a method.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class ThisExample {<\/li>\n<li dir=\"ltr\">int x;<\/li>\n<li dir=\"ltr\">int y;<\/li>\n<li dir=\"ltr\">ThisExample (int x, int y) {<\/li>\n<li dir=\"ltr\">this.x = x;<\/li>\n<li dir=\"ltr\">this.y = y;<\/li>\n<li dir=\"ltr\">System.out.println (\u201cBefore passing this to addTwo () method:\u201d);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cx =\u201d + this.x + \u201c, y =\u201d + this.y);<\/li>\n<li dir=\"ltr\">addTwo (this);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cAfter passing this to addTwo () method:\u201d);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cx =\u201d + this.x + \u201c, y =\u201d + this.y);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">void addTwo (ThisExample o) {<\/li>\n<li dir=\"ltr\">ox + = 2;<\/li>\n<li dir=\"ltr\">oy + = 2;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">class Demo {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">ThisExample obj = new ThisExample (1, -2);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">Before passing this to addTwo () method:<\/p>\n<p dir=\"ltr\">x = 1, y = -2<\/p>\n<p dir=\"ltr\">After passing this to addTwo () method:<\/p>\n<p dir=\"ltr\">x = 3, y = 0<\/p>\n<\/blockquote>\n<p>Here, the addTwo () method is called with this as an argument from within the constructor.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What does the \u201cthis\u201d keyword mean in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It refers to the current object\u2014the instance whose method or constructor is being executed.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why is the \u201cthis\u201d keyword used in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>It\u2019s mainly used to differentiate instance variables from local variables and to access current object properties or methods.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can \u201cthis\u201d be used to call constructors?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, using this() you can call another constructor within the same class to reuse initialization code.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn the keyword this in Java. An example will help you learn how and where to use it. This keyword In Java, this refers to the current object in methods or constructs.\u00a0Let&#8217;s give an example to prove it. class MyClass { int instVar; MyClass (int instVar) { this.instVar = instVar; [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5269,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5268","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5268"}],"version-history":[{"count":6,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5268\/revisions"}],"predecessor-version":[{"id":266370,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5268\/revisions\/266370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/5269"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}