go to previous page   go to home page   go to next page


Yes to both.

Coding a Constructor

Here is HelloObject with an unfinished constructor. In this constructor the parameter is an object reference.

class HelloObject                                  
  String greeting;

  HelloObject( String st )
     =  ;

  void speak()                                     
    System.out.println( greeting );

Examine the parameter list of the constructor:

String st

This says that the constructor is given a reference to a String when it is used (when it is called). The name of the parameter is st. In the body of the constructor, st represents the data. The constructor will initialize the variable greeting with data that is supplied when the constructor is used. For example, in the main() method above the String "A Greeting!" is supplied as data to the constructor.


Fill in the blank so that the constructor is complete.