Greeter.java

Download Greeter.java source file

// Greeter.java - Creating objects and invoking methods on them.
// Greeter objects have a "greet" method that takes a name and
// greets that person.  A second class is provided as well,
// "GreeterApp" that uses greeter objects.
//
// Written by Wayne Pollock, Tampa Florida USA 1/2004.

class Greeter
{
   private String message;  // A property of each Greeter object.

   // A "constructor" that is used to initialize a Greeter
   //  object's properties: the greeting message to use.

   public Greeter ( String message )
   {
      this.message = message;  // Sets the message property
                               // from the message argument
   }

   public void greet ( String name )
   {
      System.out.println( message + " " + name );
   }
}


class GreeterApp
{
   public static void main ( String [] args )
   {
      // Create some Greeter objects...
      Greeter casual = new Greeter( "Howdy" );
      Greeter formal = new Greeter( "Salutations" );

      // ... and use them!
      casual.greet( "Hymie" );
      casual.greet( "Jane" );
      formal.greet( "Mr. President" );
      formal.greet( "Caleb" );
   }
}