Greeter.java
Download Greeter.java
1: // Greeter.java - Creating objects and invoking methods on them.
2: // Greeter objects have a "greet" method that takes a name and
3: // greets that person. A second class is provided as well,
4: // "GreeterApp" that uses greeter objects.
5: //
6: // Written by Wayne Pollock, Tampa Florida USA 1/2004.
7:
8: class Greeter
9: {
10: private String message; // A property of each Greeter object.
11:
12: // A "constructor" that is used to initialize a Greeter
13: // object's properties: the greeting message to use.
14:
15: public Greeter ( String message )
16: {
17: this.message = message; // Sets the message property
18: // from the message argument
19: }
20:
21: public void greet ( String name )
22: {
23: System.out.println( message + " " + name );
24: }
25: }
26:
27:
28: class GreeterApp
29: {
30: public static void main ( String [] args )
31: {
32: // Create some Greeter objects...
33: Greeter casual = new Greeter( "Howdy" );
34: Greeter formal = new Greeter( "Salutations" );
35:
36: // ... and use them!
37: casual.greet( "Hymie" );
38: casual.greet( "Jane" );
39: formal.greet( "Mr. President" );
40: formal.greet( "Caleb" );
41: }
42: }