0
What is the use of "this" keyword in java
3 Respostas
+ 1
Refers to a field variable and not a local one
+ 1
In a java class definition, 'this' refers to the current instance of the class. (The actual instance will be created outside of the class with the 'new' keyword.)
It is used to access the instance variables and methods.
Example: it is conventional to use in getter / setter methods but also anywhere else when you refer to instance variables.
class Bucket {
// instance variable
private bool isEmpty;
// setter
public void setEmpty(bool isEmpty) {
this.isEmpty = isEmpty;
}
}
Here the isEmpty instance variable will take the value that is passed in the parameter of the setter method.