0

What is the use of This keyword in java?

20th Feb 2017, 5:36 PM
Aman Mamgain
2 Respuestas
+ 4
this refers to the current object. It is often used in constructors to not mix up parameters and current object attributes, e.g.: class MyClass { private int i; public MyClass (int i) { this.i=i; // sets parameter i to attribute i } }
20th Feb 2017, 6:27 PM
Tashi N
Tashi N - avatar
0
class Monster{ void kill(Monster monster) {...} void suicide () { kill (this) ;} } This is always the acting object. On suicide, the monster kills itself.
20th Feb 2017, 11:29 PM
1of3
1of3 - avatar