Methods can use Variables

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:

It would be nice if different objects printed different things.

Methods can use Variables

If different instances (different objects) of the HelloObject class print different strings, then each instance must have its own data, which must be initialized. The class definition needs a constructor. Here is what a class definition usually looks like:

class ClassName
{
  Description of the variables.

  Description of the constructors.

  Description of the methods.

}

Important: An object's methods use that object's instance variables. Remember that an object has identity, state, and behavior. "Identity" means that each object has its own variables. The "state" of an object is the values held in its variables. The "behavior" of an object is its methods, which use the object's own variables.

QUESTION 14:

If each instance of HelloObject had its own message, where would that message be kept?