![]() |
![]() |
![]() |
![]() |
![]() |
|
When an instance method is called, the target
argument's value is assigned to a special parameter, this.
Thus, the this parameter's value is the target class instance.
Accordingly, you can write, say, the Movie rating instance
method in two ways. One waythe way introduced in
Segment 194exploits the convention that instance variables
refer to the target instance:
public class Movie {
public int script, acting, direction;
public int rating () {
return script + acting + direction;
}
}
Another way to define rating uses the this parameter, thus
referring to the target instance explicitly:
public class Movie {
public int script, acting, direction;
public int rating () {
return this.script + this.acting + this.direction;
}
}
Some programmers use this liberally, arguing that liberal use of
this makes programs easier to understand. In this book, we use
this only when necessary, believing that liberal use of this
makes programs bulky.