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

Answer:

The near-complete class is below.


Using a Constructor

Here is the class with a String reference variable included. Of course, you may have used a different name for it.

class HelloObject                                  
{
  String greeting;

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

The class is not complete because there is no way to initialize the greeting (we will get to this shortly). It would be nice if the object could be used like this:

class HelloTester
{
  public static void main ( String[] args )        
  {
    HelloObject anObject = new HelloObject("A Greeting!"); 
    anObject.speak();
  }
}

QUESTION 16:

Where in the above code is a constructor being used? What is its parameter?