0

What is the best use of "This" keyword in java?

what is the best use of the this keyword.

9th Jan 2020, 5:27 PM
Muhammad Asad
Muhammad Asad - avatar
8 Respuestas
+ 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.
9th Jan 2020, 5:35 PM
Tibor Santa
Tibor Santa - avatar
+ 8
this keyword always represent the current object , this is a reference variable which is always pointing to current object
11th Jan 2020, 4:53 AM
Saroj Patel
Saroj Patel - avatar
+ 2
You finished the java course and haven't seen the use of this? 🤔 https://www.sololearn.com/learn/Java/2157/
9th Jan 2020, 5:33 PM
HNNX 🐿
HNNX 🐿 - avatar
+ 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...
9th Jan 2020, 6:49 PM
Jayakrishna 🇮🇳
0
I want to know more about this topic.
9th Jan 2020, 5:35 PM
Muhammad Asad
Muhammad Asad - avatar