Keyword "this" in Java
What is the use of the keyword 'this' in Java programming?
What is the meaning of "this" in Java? - Stack Overflow
coding style - Java - when to use 'this' keyword - Stack Overflow
Videos
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 :(
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
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:
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
thisdenotes the local variable. If the local variable that is parameter’s name is not same as instance variable then irrespective ofthisis used or not it denotes the instance variable.thisis used to refer the constructorspublic JavaQuestions(String javapapers) { this(javapapers, true); }This invokes the constructor of the same java class which has two parameters.
thisis used to pass the current java instance as parameterobj.itIsMe(this);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.
thiscan be used to get the handle of the current classClass 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.
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
You should use it when you have a parameter with the same name as a field otherwise you will run into issues. It will compile, but won't necessarily do what you want it to.
As for everywhere else, don't use it unless it's needed for readability's sake. If you use it everywhere, 20% of your code will consist of the word 'this'!