0
What is the use of This keyword in java?
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
}
}
0
class Monster{
void kill(Monster monster) {...}
void suicide () { kill (this) ;}
}
This is always the acting object. On suicide, the monster kills itself.