The this keyword is a reference to the current object.

class Foo
{
    private int bar;

    public Foo(int bar)
    {
        // the "this" keyword allows you to specify that
        // you mean "this type" and reference the members
        // of this type - in this instance it is allowing
        // you to disambiguate between the private member
        // "bar" and the parameter "bar" passed into the
        // constructor
        this.bar = bar;
    }
}

Another way to think about it is that the this keyword is like a personal pronoun that you use to reference yourself. Other languages have different words for the same concept. VB uses Me and the Python convention (as Python does not use a keyword, simply an implicit parameter to each method) is to use self.

If you were to reference objects that are intrinsically yours you would say something like this:

My arm or my leg

Think of this as just a way for a type to say "my". So a psuedocode representation would look like this:

class Foo
{
    private int bar;

    public Foo(int bar)
    {
        my.bar = bar;
    }
}
Answer from Andrew Hare on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-this-keyword
Java this Keyword - GeeksforGeeks
In Java, "this" is a reference variable that refers to the current object, or can be said "this" in Java is a keyword that refers to the current object instance.
Published   1 month ago
🌐
W3Schools
w3schools.com › java › ref_keyword_this.asp
Java this Keyword
The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).
Discussions

Using the keyword "this" in java - Stack Overflow
I'm trying to get an understanding of what the the java keyword this actually does. I've been reading Sun's documentation but I'm still fuzzy on what this actually does. More on stackoverflow.com
🌐 stackoverflow.com
What is the meaning of "this" in Java? - Stack Overflow
However, on the most fundamental ... to this inside it. 2014-09-14T09:53:44.103Z+00:00 ... @JoachimSauer If I wanted to run it, how would I? I've got the same problem as dbconfession. 2017-12-24T19:57:48.197Z+00:00 ... The following is a copy & paste from here, but explains very well all different uses of the this keyword: Definition: Java’s this keyword ... More on stackoverflow.com
🌐 stackoverflow.com
What is the accepted style for using the `this` keyword in Java? - Software Engineering Stack Exchange
I come from languages like Python or Javascript (and others that are less object-oriented) and I am trying to improve my working knowledge of Java, which I know only in a superficial way. Is it More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
October 10, 2011
What is the use of the keyword 'this' in Java programming?
The 'this' keyword refers to the current object in a function or constructor. More on knowledgeboat.com
🌐 knowledgeboat.com
1
7
January 9, 2021
🌐
Reddit
reddit.com › r/learnjava › when to use the this. keyword in java ? [complete noob still]
r/learnjava on Reddit: when to use the this. keyword in java ? [complete noob still]
August 3, 2022 -

can someone help me out on better understanding on when to use the this. keyword ?

I have this code right here:

    public class ATM { 
        public static int totalMoney = 0; 
        public static int numATMs = 0; 
    
        public int money; 
    
        public ATM(input money){
            this.money = inputMoney;
    
            numATMs += 1; 
            totalMoney += inputMoney; 
        }
    
        public void withdrawMoney(int amountToWithdraw){
            if (amountToWithdraw <= this.money){
                this.money -= amountToWithdraw; 
                totalMoney -= amountToWithdraw; 
            }
        }
    
        public static void main(String[] args) {
            System.out.println
        }
    }

and i understand the majority of this right here. However the part that im not understanding is this part:

    public void withdrawMoney(int amountToWithdraw){
        if (amountToWithdraw <= this.money){
            this.money -= amountToWithdraw; 
            totalMoney -= amountToWithdraw; 
        }
    }

to my understanding, the word this. basically says "we are using THIS variable, in this case money within this method" and the reason that we don't have THIS on amountToWithdraw is basically a global variable that we are accessing ?

sorry for the dumb question. please don't roast me too hard :(

Top answer
1 of 12
39

The this keyword is a reference to the current object.

class Foo
{
    private int bar;

    public Foo(int bar)
    {
        // the "this" keyword allows you to specify that
        // you mean "this type" and reference the members
        // of this type - in this instance it is allowing
        // you to disambiguate between the private member
        // "bar" and the parameter "bar" passed into the
        // constructor
        this.bar = bar;
    }
}

Another way to think about it is that the this keyword is like a personal pronoun that you use to reference yourself. Other languages have different words for the same concept. VB uses Me and the Python convention (as Python does not use a keyword, simply an implicit parameter to each method) is to use self.

If you were to reference objects that are intrinsically yours you would say something like this:

My arm or my leg

Think of this as just a way for a type to say "my". So a psuedocode representation would look like this:

class Foo
{
    private int bar;

    public Foo(int bar)
    {
        my.bar = bar;
    }
}
2 of 12
21

The keyword this can mean different things in different contexts, that's probably the source of your confusion.

It can be used as a object reference which refers to the instance the current method was called on: return this;

It can be used as a object reference which refers to the instance the current constructor is creating, e.g. to access hidden fields:

MyClass(String name)
{
    this.name = name;
}

It can be used to invoke a different constructor of a a class from within a constructor:

MyClass()
{
    this("default name");
}

It can be used to access enclosing instances from within a nested class:

public class MyClass
{
    String name;

    public class MyClass
    {
        String name;

        public String getOuterName()
        {
            return MyClass.this.name;
        }
    }
}
🌐
Quora
quora.com › What-is-the-purpose-of-the-this-keyword-in-Java
What is the purpose of the 'this' keyword in Java? - Quora
Answer (1 of 4): “this” has two functions. The easier one first, is in a constructor. Let's say you have class MyClass{ int someVar; public MyClass(int x) { someVar=x; } public MyClass() { this(1); } So the second constructor is calling the first constructor. In Java the incantation fo...
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › thiskey.html
Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x. From within a constructor, you can also use the this keyword to call another constructor in the same class.
🌐
YouTube
youtube.com › watch
This Keyword in Java Full Tutorial - How to Use "this" - YouTube
What does the "this" keyword mean in Java? How and when do you use "this"? We'll cover all of that (and all of "this") in this video.In Java, the keyword "th...
Published   October 18, 2021
Top answer
1 of 16
171

this refers to the current object.

Each non-static method runs in the context of an object. So if you have a class like this:

public class MyThisTest {
  private int a;

  public MyThisTest() {
    this(42); // calls the other constructor
  }

  public MyThisTest(int a) {
    this.a = a; // assigns the value of the parameter a to the field of the same name
  }

  public void frobnicate() {
    int a = 1;

    System.out.println(a); // refers to the local variable a
    System.out.println(this.a); // refers to the field a
    System.out.println(this); // refers to this entire object
  }

  public String toString() {
    return "MyThisTest a=" + a; // refers to the field a
  }
}

Then calling frobnicate() on new MyThisTest() will print

1
42
MyThisTest a=42

So effectively you use it for multiple things:

  • clarify that you are talking about a field, when there's also something else with the same name as a field
  • refer to the current object as a whole
  • invoke other constructors of the current class in your constructor
2 of 16
55

The following is a copy & paste from here, but explains very well all different uses of the this keyword:

Definition: Java’s this keyword is used to refer the current instance of the method on which it is used.

Following are the ways to use this:

  1. To specifically denote that the instance variable is used instead of static or local variable. That is,

    private String javaFAQ;
    void methodName(String javaFAQ) {
        this.javaFAQ = javaFAQ;
    }
    

    Here this refers to the instance variable. Here the precedence is high for the local variable. Therefore the absence of the this denotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective of this is used or not it denotes the instance variable.

  2. this is used to refer the constructors

     public JavaQuestions(String javapapers) {
         this(javapapers, true);
     }
    

    This invokes the constructor of the same java class which has two parameters.

  3. this is used to pass the current java instance as parameter

    obj.itIsMe(this);
    
  4. Similar to the above this can also be used to return the current instance

    CurrentClassName startMethod() {
         return this;
    }
    

    Note: This may lead to undesired results while used in inner classes in the above two points. Since this will refer to the inner class and not the outer instance.

  5. this can be used to get the handle of the current class

    Class className = this.getClass(); // this methodology is preferable in java
    

Though this can be done by

    Class className = ABC.class; // here ABC refers to the class name and you need to know that!

As always, this is associated with its instance and this will not work in static methods.

Find elsewhere
🌐
DataCamp
datacamp.com › doc › java › this
this Keyword in Java: Usage & Examples
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming ... The this keyword in Java is a reference variable that refers to the current object.
🌐
Quora
quora.com › What-are-this-and-other-in-Java-What-do-they-do
What are 'this' and 'other' in Java? What do they do? - Quora
Answer (1 of 7): You’ll often see this in my code: [code]class User { public boolean equals( Object other ) { return EqualsBuilder.reflectionEquals( this, other ); } } [/code]‘this’ refers to the current object instance the ‘equals’ method is part of. It always means that. It is a keyw...
🌐
GeeksforGeeks
geeksforgeeks.org › java › super-and-this-keywords-in-java
super and this keywords in Java - GeeksforGeeks
June 10, 2024 - We can use this as well as super any number of times in a program. Both are non-static keyword. ... // Java Program to illustrate using this // many number of times class RRR { // instance variable int a = 10; // static variable static int b = 20; void GFG() { // referring current class(i.e, class RR) // instance variable(i.e, a) this.a = 100; System.out.println(a); // referring current class(i.e, class RR) // static variable(i.e, b) this.b = 600; System.out.println(b); // referring current class(i.e, class RR) // instance variable(i.e, a) again this.a = 9000; System.out.println(a); } public static void main(String[] args) { new RRR().GFG(); } }
🌐
W3Schools
w3schools.com › java › java_ref_keywords.asp
Java Keywords
Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Constructors Java this Keyword Java Modifiers · Access Modifiers Non-Access Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java super Keyword Java Inner Classes Java Abstraction ...
🌐
Medium
medium.com › @AlexanderObregon › understanding-the-this-keyword-in-java-a-key-interview-question-b0381840b583
Understanding the “this” Keyword in Java — Interview Question
April 24, 2024 - The this keyword in Java is a reference variable that refers to the current object of a class. It's primarily used within instance methods or constructors to distinguish the class's instance variables from local variables when they share the ...
🌐
Programiz
programiz.com › java-programming › this-keyword
Java this Keyword
In the above example, inside the constructor ThisExample(), notice the line, ... Here, we are calling the add() method by passing this as an argument. Since this keyword contains the reference to the object obj of the class, we can change the value of x and y inside the add() method.
🌐
DEV Community
dev.to › arshisaxena26 › mastering-this-keyword-in-java-a-key-to-clean-and-effective-code-3pad
Mastering THIS Keyword in Java: A Key to Clean and Effective Code - DEV Community
November 6, 2024 - The this keyword in Java serves as a reference to the current object. It enhances code readability and ensures precise control over constructors, instance variables, and method calls.
🌐
Wikipedia
en.wikipedia.org › wiki › List_of_Java_keywords
List of Java keywords - Wikipedia
October 20, 2025 - This keyword can also be used to create an if-else statement; see else. ... Included in a class declaration to specify one or more interfaces that are implemented by the current class. A class inherits the types and abstract methods declared by the interfaces. ... Used at the beginning of a source file to specify classes or entire Java packages to be referred to later without including their package names in the reference.
Top answer
1 of 16
478

The this keyword is primarily used in three situations. The first and most common is in setter methods to disambiguate variable references. The second is when there is a need to pass the current class instance as an argument to a method of another object. The third is as a way to call alternate constructors from within a constructor.

Case 1: Using this to disambiguate variable references. In Java setter methods, we commonly pass in an argument with the same name as the private member variable we are attempting to set. We then assign the argument x to this.x. This makes it clear that you are assigning the value of the parameter "name" to the instance variable "name".

public class Foo
{
    private String name;

    public void setName(String name) {
        this.name = name;
    }
}

Case 2: Using this as an argument passed to another object.

public class Foo
{
    public String useBarMethod() {
        Bar theBar = new Bar();
        return theBar.barMethod(this);
    }

    public String getName() {
        return "Foo";
    }
}

public class Bar
{
    public void barMethod(Foo obj) {
        obj.getName();
    }
}

Case 3: Using this to call alternate constructors. In the comments, trinithis correctly pointed out another common use of this. When you have multiple constructors for a single class, you can use this(arg0, arg1, ...) to call another constructor of your choosing, provided you do so in the first line of your constructor.

class Foo
{
    public Foo() {
        this("Some default value for bar");

        //optional other lines
    }

    public Foo(String bar) {
        // Do something with bar
    }
}

I have also seen this used to emphasize the fact that an instance variable is being referenced (sans the need for disambiguation), but that is a rare case in my opinion.

2 of 16
81

The second important use of this (beside hiding with a local variable as many answers already say) is when accessing an outer instance from a nested non-static class:

public class Outer {
  protected int a;

  public class Inner {
    protected int a;

    public int foo(){
      return Outer.this.a;
    }

    public Outer getOuter(){
      return Outer.this;
    }
  }
}
🌐
ScholarHat
scholarhat.com › home
This Keyword in Java
September 9, 2025 - In Java, "this" keyword is a reference to the current object within a class. It is used to differentiate between instance variables and parameters when they have the same name and to invoke constructors or methods from within the same class.