There is java source code all over this blog and the internet. I’ve been a java programmer for two years now and I’ve seen my fair share of well written and poorly written code. One thing I always find, no wait, I actually don’t find, is the keyword this. So, Why is this so confusing in Java? A lot of people apparently wonder about this too.
Coming from another Perspective
It’s not confusing for a programmer that comes from another perspective. I code in javascript and PHP regularly. In javascript, this refers to the object the method is in. In PHP, guess what, this refers to the object the method is in. So what does it matter? What is this after all?
Let’s Use This
I provide here an example that is a class. BankAccounts have a single data member, as double called amount. We’ll see how this saves us.
public class BankAccount {
private double amount = 100;
public double getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
So what is the big deal? Checkout line 10. Notice how there is a parameter named amount? Also notice on line 2 there is a private double called amount. This is not a conflict. The amount in the class, the data member, called amount, is separate from the parameter in the setAmount method.
Now, look for this in the setAmount method. You’ll see it on line 9, this.amount = amount. Why do I use this? To be clear of the scope. The amount from the parameter is accessible within the setAmount method while the amount from the class, the data member, is accessible everywhere. If you hadn’t declared this, you would be literally assigning the parameter to itself.
So, this refers to the object. It clarifies the scope. If you have data members, data fields, or instance variables, regardless of what you call them, if you reference them inside of a method, please use the this.instance_variable notation. It really speeds up code reading.

Thanks, I was wondering about this.