Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > Getting Started With WebObjects


   

Creating the Guest Object

Earlier in this chapter, you created a Java class of type Guest and wrote a constructor for it. You also added a variable of that class, currentGuest, to the Main component. However, adding a variable to the component doesn't actually create a new Guest object; you need to create one explicitly at some point in your code.

You'll create the Guest object in the constructor method for your component. This method is called when the component is first created; that is, the first time the user accesses the component.

Note: In WebScript or Objective-C, you use a method called init for this purpose.

  1. Choose View Source File from the pull-down list at the bottom of the window.

    Project Builder becomes active and displays the code for Main.java. Notice the following declaration that was added to your code when you added the currentGuest variable:

    protected Guest currentGuest;

  2. Delete the declarations of guestName, email, and comments since you aren't using them anymore.

  3. Add the constructor method inside the Main class definition:

    public Main() {
       super();
       currentGuest = new Guest();
    }

    The first statement calls the constructor of Main's superclass (which is com.apple.yellow.webobjects.WOComponent). The second statement allocates a new empty Guest object and calls Guest's constructor to initialize its instance variables.

  4. Save Main.java.

  5. Build and run your application.

    The application should work similarly to that in the first chapter, except that the guest's data is displayed in a table at the bottom of the page instead of as plain text.

    At this point, your application still handles information from a single guest only; in the next section, you'll modify the application so that it can keep track of multiple guests.


© 1999 Apple Computer, Inc. – (Last Updated 24 Aug 99)