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


   

Creating a Custom Guest Class

In the first chapter, you created individual variables to store a guest's name, e-mail address, and comments. When keeping track of multiple guests, it's more useful to encapsulate all the data for a guest as a single entity. You'll do this by creating a Java class that contains the data for a single guest.

  1. In Project Builder's browser, select Classes in the first column.

  2. Choose File   New in Project.

  3. Type Guest.java as the name of the file.

  4. Click OK.
  5. The newly created file contains a skeleton for a class called Guest.

  6. Modify the code so it looks like this:
  7. // Generated by the WebObjects Wizard ...

    import com.apple.yellow.foundation.*;
    import com.apple.yellow.eocontrol.*;
    import com.apple.yellow.webobjects.*;

    public class Guest extends EOCustomObject {
       protected String guestName;
       protected String email;
       protected String comments;

       Guest() {
         guestName = "";
         email = "";
         comments = "";
       }

    }

    A class stores information in its instance variables (also referred to as data members). Here you're declaring three instance variables for Guest: guestName, email, and comments. Note that these declarations are the same as those that appeared in the code for Main.java when you added the three variables using WebObjects Builder. In WebObjects, a component is also a class, specifically a subclass of the class WOComponent.

    Java classes require a constructor to initialize an instance (or object) of a particular class whenever one is created. A constructor has the same name as the class and returns no value.

    Whenever your application creates a new Guest class, its instance variables are initialized with empty strings, which is the default value if the user enters no data. (If you prefer, you can use different strings for these initial values.)

  8. Save Guest.java by choosing Save from the File menu.

    Saving the file lets WebObjects Builder know about your newly created Guest class.

Binding the Class's Instance Variables
to the Form Elements

Creating a Table to Display the Output

Adding Dynamic Elements to Table Cells

Binding the Dynamic Elements in the Table

Creating the Guest Object


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