It's not always just a naming convention -- that was not the best way to explain it. The this keyword is used to overcome shadowing. The this keyword is used to refer to the field name - if the field name and parameter name are the same. Some programmers don't like to think of a different name. So they will use name = name , but this will result in shadowing -- since both have the same name. This can be overcome by using mName = name OR this.name = name; In Android they use mName = name; whereas a Java Developer will most likely use this.name = name; The this keyword is also used when chaining constructors (aka explicit constructor invocation) -- that's when a constructor is called from another constructor. This is a keyword that an object can use to refer to itself -- that's how it is used in Java. If you would like me to explain anything else I will be happy to do so. Answer from Michael Hess on teamtreehouse.com
🌐
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).
🌐
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   4 weeks ago
Discussions

Keyword "this" in Java
Sudharaka Palamakumbura is having issues with: I have seen sometimes when referring to a method of a class within the class itself programmers use the keyword "this". Here in the Treehouse Ja... More on teamtreehouse.com
🌐 teamtreehouse.com
2
May 8, 2015
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 12, 2021
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
coding style - Java - when to use 'this' keyword - Stack Overflow
That's fine and all, but Java is clever enough to know what is happening if I change the statement in the constructor to ... So why use the this keyword? (I realise in some situations, it's totally necessary to use it, I'm just asking for situations like this). More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 :(

🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-keywords
Java Keywords - GeeksforGeeks
4 days ago - ./Geeks.java:5: error: ';' expected · String this = "Hello World!"; 2 errors · Important Points: The keywords const and goto are reserved, even though they are not currently used in Java. true, false, and null look like keywords, but in actuality they are literals.
Top answer
1 of 2
4
It's not always just a naming convention -- that was not the best way to explain it. The this keyword is used to overcome shadowing. The this keyword is used to refer to the field name - if the field name and parameter name are the same. Some programmers don't like to think of a different name. So they will use name = name , but this will result in shadowing -- since both have the same name. This can be overcome by using mName = name OR this.name = name; In Android they use mName = name; whereas a Java Developer will most likely use this.name = name; The this keyword is also used when chaining constructors (aka explicit constructor invocation) -- that's when a constructor is called from another constructor. This is a keyword that an object can use to refer to itself -- that's how it is used in Java. If you would like me to explain anything else I will be happy to do so.
2 of 2
4
In Android, m stands for member -- so it's really more of a naming convention thing in Android. It also goes along with the Android track on treehouse. The this keyword is used to overcome shadowing. The this keyword is used to refer to the field name - if the field name and parameter name are the same. For example: ```java public Customer(String title, String firstName, String surname,) { this.title = title; this.firstName = firstName; this.surname = surname; } ``` is equivalent to: ```java public Customer(String title, String firstName, String surname,) { mTitle = title; mFirstName = firstName; mSurname = surname; } ``` If you have any other questions ask away!
Find elsewhere
🌐
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...
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.

🌐
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.
🌐
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.
🌐
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_this.asp
Java this
Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The this keyword in Java refers to the current object in a method or constructor.
🌐
Talent Battle
talentbattle.in › learn-java-for-free › this-keyword-in-java
This Keyword in Java | Free Java Course - Talent Battle
August 24, 2024 - Definition: The 'this' keyword in Java refers to the current object or instance of a class.
🌐
Scaler
scaler.com › topics › java › this-and-super-keyword-in-java
This and Super Keyword in Java - Scaler Topics
May 7, 2024 - If a user wants to call a parent or a current class constructor using the “super()” or “this()” keyword, then it must be the first statement inside the constructor, but both “super()” and “this()” cannot be the first statement simultaneously due to this reason “this()” and “super()” keywords cannot be used together. ... A: We use the super keyword to refer to the parent class instance, to invoke parent class methods, static and instance variables. The “super()” is used to invoke the immediate parent class constructor. ... A: Java enforces that the call to super (explicit or not) must be the first statement in the constructor.
🌐
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.
🌐
Hero Vired
herovired.com › home › learning-hub › blogs › this-keyword-in-java
What is `this` Keyword in Java? Explained Uses with Examples
July 2, 2024 - ... Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems. ... The keyword ‘this’ is employed to denote the method associated with the current object.
🌐
Medium
medium.com › @nikhilsalvi011 › this-keyword-in-java-46df1e2b62cc
this keyword in Java. There can be a lot of usage of Java… | by Nikhil Sambhaji Salvi | Medium
October 8, 2023 - this keyword in Java There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object. Usage of Java this keyword : Invoke current class …