Improved Class

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 reference to the message is kept in one of the object's variables.

Improved Class

Here is what we have so far for HelloObject:

class HelloObject                                  
{                                                  
  void speak()                                     
  { 
    System.out.println("Hello from an object!");
  }
}

Here is a start on improving HelloObject:

class HelloObject                                  
{                                                  

   ;   // reference variable for the object's message

  void speak()                                     
  { 
    System.out.println(    );   // print the object's message
  }
}

QUESTION 15:

Modify the definition of HelloObject to include a String reference variable. Modify the speak() method so that it uses that variable.