Home Segments Top Top Previous Next

295: Mainline

Whenever you want a constructor to hand arguments to another constructor in the same class, you modify the calling constructor's definition by adding a statement consisting of the this keyword followed by an argument list. That added statement must be the first statement in the calling constructor. For example, you modify the four-parameter constructor to call the three-parameter constructor as follows:

public class Movie extends Attraction { 
 public int script, acting, direction; 
 public Movie () {script = 5; acting = 5; direction = 5;} 
 public Movie (int s, int a, int d) {      <-------* 
  script = s; acting = a; direction = d;           | Call to  
 }                                                 | three-parameter 
 public Movie (int s, int a, int d, int m) {       | constructor 
  this(s, a, d);  ---------------------------------* 
  minutes = m;  
 }                     
 public int rating () {return script + acting + direction;} 
}