---
title: "this keyword in Java"  
description: "In this article I am explaining to use this keywords in Java"  
author: "Manoj Pandey"  
published: 2015-04-22  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1735/this-keyword-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# this keyword in Java

The this [keyword](https://yourviews.mindstick.com/view/87345/explained-the-importance-of-keyword-traffic) is used when you need to use class [global variable](https://answers.mindstick.com/qa/104739/define-global-variable) in the constructors. this is a [reference](https://www.mindstick.com/forum/34619/call-by-value-and-call-by-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 this.\

In java, this is a reference variable that refers to the current object. It is [useful](https://www.mindstick.com/articles/12694/how-dolomite-mineral-is-useful-in-human-life) for a method to reference [instance variables](https://www.mindstick.com/forum/34631/instance-variables-and-class-variables) [relative](https://answers.mindstick.com/qa/105067/how-to-trade-with-the-relative-vigor-index) to this keyword. this is a keyword in Java. Which can be used inside method or constructor of class. It (this) works as a reference to current object whose method or constructor is being invoked. this keyword can be used to refer any member of current object from within an instance method or a constructor.

![this keyword in Java](https://www.mindstick.com/mindstickarticle/e66010d6-779a-4d17-a099-9e529109d819/images/59623f69-a640-4330-aa04-944be071e310.png)

The this keyword can be used to refer current class instance variable.

If there is ambiguity between the instance variable and parameter, this keyword resolves the problem of ambiguity.

##### this keyword with field(Instance Variable) -:

this keyword can be very useful in case of Variable Hiding. We can not create two Instance/Local Variable with same name. But it is legal to create One Instance Variable & One Local Variable or Method parameter with same name. In this scenario Local Variable will hide the Instance Variable which is called Variable Hiding.

##### Using this with a Constructor

```
public class Rectangle {    private int x, y;    private int width, height;            public Rectangle() {        this(0, 0, 1, 1);    }    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;    }    }
```

Example of this keyword as method parameter

```
public class Sample1 {  public static void main(String[] args) { Sample2 obj = new Sample2 (); obj.i = 10; obj.method(); } } class Sample2 extends Sample1 { int i;  void method() { method1(this); }  void method1(Sample2  t) { System.out.println(t.i); }}
```

---

Original Source: https://www.mindstick.com/articles/1735/this-keyword-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
