<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">There can be a lot of usage of <i><b>this </b></i>keyword. In java, <b><i>this </i></b>is a reference variable that refers to the current object. Within an instance method or a constructor, <b><i>this </i></b>is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using <i><b>this</b></i>.</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/thisr.jpg"/></div>
<p><b>Usage of <i>this </i>keyword</b><br />
Here is given the 6 usage of this keyword.</p>
<ol style="text-align: left;">
<li><b><i>this </i></b>keyword can be used to refer current class instance variable.</li>
<li><i><b>this()</b></i> can be used to invoke current class constructor.</li>
<li><i><b>this </b></i>keyword can be used to invoke current class method (implicitly)</li>
<li><i><b>this </b></i>can be passed as an argument in the method call.</li>
<li><i><b>this </b></i>can be passed as argument in the constructor call.</li>
<li><b><i>this </i></b>keyword can also be used to return the current class instance.</li>
</ol>
<p><b>Using <i>this </i>with a Field</b></p>
<div align='center' id="ads-id"></div>
<p>The most common reason for using the <i><b>this </b></i>keyword is because a field is shadowed by a method or constructor parameter.</p>
<p> For example, the Point class was written like this</p>
<pre class="highlight" name="code">public class Point { 
 public int x ; 
 public int y ; 
 
 //constructor 
 public Point(int x, int y) { 
 x = x; 
 y = y; 
 } 
 void display(){ 
 System.out.println(x+" :: "+y); 
 } 
 public static void main(String args[]){ 
 Point s1 = new Point (111,222); 
 Point s2 = new Point (333,444); 
 s1.display(); 
 s2.display(); 
 } 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>output:</b><br />
0 :: 0<br />
0 :: 0</div>
<p>
In the above example, parameter (formal arguments) and instance variables are same that is why we are using <b><i>this </i></b>keyword to distinguish between local variable and instance variable. </p>
<p> but it could have been written like this:</p>
<pre class="highlight" name="code">public class Point { 
 public int x = 0; 
 public int y = 0; 
 
 //constructor 
 public Point(int x, int y) { 
 this.x = x; 
 this.y = y; 
 } 
 void display(){ 
 System.out.println(x+" :: "+y); 
 } 
 public static void main(String args[]){ 
 Point s1 = new Point (111,222); 
 Point s2 = new Point (333,444); 
 s1.display(); 
 s2.display(); 
 } 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>output:</b><br />
111 :: 222<br />
333 :: 444</div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/thisvalue.jpg"/></div>
<p>
</div>
<p>
<b>Using this with a Constructor</b></p>
<p> From within a constructor, you can also use the <b><i>this </i></b>keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation. Here&#8217;s another Rectangle class, with a different implementation from the one in the Objects section.</p>
<pre class="highlight" name="code">public class Rectangle { 
 private int x, y; 
 private int width, height; 
 
 public Rectangle() { 
 this(0, 0, 0, 0); 
 } 
 public Rectangle(int width, int height) { 
 this(0, 0, width, height); 
 } 
 public Rectangle(int x, int y, int width, int height) { 
 this.x = x; 
 this.y = y; 
 this.width = width; 
 this.height = height; 
 } 
 ... 
} 
</pre>
<p>This class contains a set of constructors. Each constructor initializes some or all of the rectangle&#8217;s member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor calls the four-argument constructor with four 0 values and the two-argument constructor calls the four-argument constructor with two 0 values. As before, the compiler determines which constructor to call, based on the number and the type of arguments.</p>
<p>If present, the invocation of another constructor must be the first line in the constructor.</p>
<p> <b>Where to use <i>this()</i> constructor call?</b><br />
The <b><i>this()</i></b> constructor call should be used to reuse the constructor in the constructor. It maintains the chain between the constructors i.e. it is used for constructor chaining. Let&#8217;s see the example given below that displays the actual use of <b><i>this </i></b>keyword.</p>
<pre class="highlight" name="code">class Employee{ 
 int id; 
 String name; 
 String city; 
 
 Employee(int id,String name){ 
 this.id = id; 
 this.name = name; 
 } 
 Employee(int id,String name,String city){ 
 this(id,name);//now no need to initialize id and name 
 this.city=city; 
 } 
 void display(){ 
 System.out.println(id+" "+name+" "+city); 
 } 
 
 public static void main(String args[]){ 
 Employee e1 = new Employee(222,"Dinesh"); 
 Employee e2 = new Employee(555,"Anamika","Noida"); 
 e1.display(); 
 e2.display(); 
 } 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
222 Dinesh null<br />
555 Anamika Noida</div>
<p><i><br />
</i> <b><i>Note:</i> Call to this() must be the first statement.</b></p>
<pre class="highlight" name="code">class Employee{ 
 int id; 
 String name; 
 Employee(){ 
 System.out.println("default constructor is invoked"); 
 } 
 
 Employee(int id,String name){ 
 id = id; 
 name = name; 
 this ();//must be the first statement 
 } 
 void display(){ 
 System.out.println(id+" "+name); 
 } 
 
 public static void main(String args[]){ 
 Employee e1 = new Employee(222,"Dinesh"); 
 Employee e2 = new Employee(555,"Anamika"); 
 e1.display(); 
 e2.display(); 
 } 
} 
</pre>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
Compile Time Error</div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/final-keyword-in-java/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/super-keyword-in-java/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/final-keyword-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/super-keyword-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '507'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…