0
What is the best use of "This" keyword in java?
what is the best use of the this keyword.
8 Respostas
+ 2
Typically this keyword is used in setter methods and constructors, when setting or initializing the instance variable based on the value given in argument.
private int number;
public void setNumber(int number) {
this.number = number;
}
In the setter method, this.number means the private instance variable, and number is the function argument. It is a convention that they are called the same and hence this. prefix clearly distinguishes which is which.
+ 8
this keyword always represent the current object , this is a reference variable which is always pointing to current object
+ 2
You finished the java course and haven't seen the use of this? 🤔
https://www.sololearn.com/learn/Java/2157/
+ 1
this refers to current object.
For ex :
public void setColor(String c) {
this.color = c;}
Class1 obj= new Class1();
We call methods through objects like
c.setColor("cyan");
This will call above method..
cyan will set to 'obj' object's color value.
By referring this.color =c
Hence obj.color="cyan"
Here current object is obj.
obj=this
obj.color=this.color...
0
I want to know more about this topic.