Completed 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:

A complete program is below.

Completed Constructor

Here is a complete program. The constructor in the definition of HelloObject has a parameter named st. This parameter is used to pass data into the constructor. The constructor itself does nothing but copy it to the variable greeting of the object.

class HelloObject                                  
{
  String greeting;

  HelloObject( String st )
  {
    greeting = st;
  }

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

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

QUESTION 19:

What will this program print on the monitor when it is run?