but Java is clever enough to know what is happening if I change the statement in the constructor to

  bar = bar;

FALSE! It compiles but it doesn't do what you think it does!

As to when to use it, a lot of it is personal preference. I like to use this in my public methods, even when it's unnecessary, because that's where the interfacing happens and it's nice to assert what's mine and what's not.

As reference, you can check the Oracle's Java Tutorials out about this.subject ;-)

http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

Answer from polygenelubricants on Stack Overflow
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ java โ€บ this
this Keyword in Java: Usage & Examples
Here, the this keyword is used ... = new ThisConstructorExample(); obj.display(); } } In this example, the this keyword is used to call another constructor from within a constructor....
๐ŸŒ
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).
๐ŸŒ
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 ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-this-keyword
Java this Keyword - GeeksforGeeks
The this keyword can be used to access instance variables and methods of the object on which the method or constructor is being invoked. ... // Java program to demonstrate // this reference // Driver Class public class Person { // Fields Declared ...
Published ย  January 12, 2016
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ this-keyword
Java this Keyword
In the above example, we created an object named obj of the class Main. We then print the reference to the object obj and this keyword of the class. Here, we can see that the reference of both obj and this is the same. It means this is nothing but the reference to the current object.
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
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 01 โ€บ this-keyword-java-example-tutorial.html
10 Example of this keyword in Java
5) this is a final variable in Java and you can not assign value to this. this will result in a compilation error: this = new Loan(); //cannot assign value to final variable : this ยท 6) you can call methods of the class by using this keyword as shown in the below example.
๐ŸŒ
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 :(

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ super-and-this-keywords-in-java
super and this keywords in Java - GeeksforGeeks
June 10, 2024 - Example of this is already shown above where we use this as well as super inside public static void main(String[]args) hence we get Compile Time Error since cannot use them inside static area. 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(); } }
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ java
What is `this` keyword in Java? Explained with examples
March 16, 2023 - In C programming, the global variable and local variable share the same value, and preference is given to the local variable, but in Java, we can display the local variables and instance variables using this keyword.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ this-keyword
this keyword in Java
this keyword in java or java this keyword is used to refer the current object. use of this keyword in java, uses of java this keyword, understanding the problem without this keyword.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ this_statement_in_java.htm
Java - this keyword
public class JavaTester { private String message; public JavaTester(String message){ this.message = message; } public void printMessage(){ System.out.println(message); } public static void main(String args[]) { JavaTester tester = new JavaTester("Hello World"); tester.printMessage(); } } Compile and run the above program. This will produce the following result โˆ’ ... Following example shows a usecase of this keyword in case of inheritance.
๐ŸŒ
CodeJava
codejava.net โ€บ java-core โ€บ the-java-language โ€บ this-keyword
Java this keyword examples
Manager m1 = new Manager();In this context, this.employees refer to the array variable Employee[] of the instance m1, and this.report() refers to a method of the instance m1.In Java, the this keyword is often used to assign values for instance variables in constructor, getter and setter methods.
๐ŸŒ
Scientech Easy
scientecheasy.com โ€บ home โ€บ blog โ€บ this keyword in java
This Keyword in Java - Scientech Easy
April 25, 2025 - In the above example program, the parameterโ€™s identifier (formal arguments) is the same as that of the instance variable. It is permissible to do this in Java. But, there occurs the problem of ambiguity between parameters and instance variables. So, we can resolve this problem by using this keyword in Java programming.
๐ŸŒ
Vaia
vaia.com โ€บ java this keyword
Java This Keyword: Explanation & Examples | Vaia
In this example, calling this(type, 4) within the first constructor effortlessly initializes the Vehicle object, saving both time and lines of code. The this keyword is a crucial tool in Java, streamlining coding practices in object-oriented programming by referencing the current object instance.
๐ŸŒ
Medium
medium.com โ€บ javarevisited โ€บ this-keyword-in-java-e4718aa153f2
'this' Keyword in Java
November 30, 2022 - ... Let's suppose we have a program ... scope. class Ambiguity { public static void main(String[] args) { Opinion op = new Opinion(); op.print(); } } class Opinion{ int opt = 10; public void print(){ String opt = "Make an opinion"; System.out.println(opt); } } So when you ...
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ java tutorials โ€บ this keyword in java
this Keyword in Java
November 26, 2024 - Whether โ€œaโ€ on the left side of the assigned operator is the instance variable or the local variable. Hence, it does not set the value of โ€˜aโ€™ when the method set data is called. ... Append both โ€˜aโ€™ and โ€˜bโ€™ with the Java this keyword followed by a dot (.) operator.
๐ŸŒ
Studytonight
studytonight.com โ€บ java โ€บ this-keyword-in-java.php
this keyword in Java | Core Java Tutorial | Studytonight
This is another use of this keyword that allows to access method. We can access method using object reference too but if we want to use implicit object provided by Java then use this keyword. ... In this example, we are accessing getName() method using this and it works fine as works with object ...