Using a Constructor

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
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 ince if the object could 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?