Search⌘ K

Designing a New Class

Explore the steps to design a Java class including naming the class and data fields, specifying and describing behaviors with methods, and understanding the role of constructors. Learn how to write method headers, use the toString method, and prepare client code statements to test and refine your class design before implementation.

Design steps

Imagine that your current programming project involves the names of people. You could represent the names as strings, but you would prefer that they were objects of a data type specifically designed for names. You decide to create your own data type. You do this by writing a class definition.

In designing a class, you need to take the following steps:

  1. Name the class.
  2. Choose and name the data fields, indicating their data types.
  3. Describe the behaviors associated with objects of the class.
  4. Name the methods that will implement these behaviors.
  5. Specify the methods by writing a header and descriptive comments for each one.
  6. Write statements that use the methods and could appear in a client.
  7. Revise the method specifications, if necessary.

You would implement the class—by declaring the data fields and completing the body of each method—only after you are satisfied with the class design.

Let’s design the class Name.

Choosing the behaviors

For simplicity, we assume that a person’s name ...