Summary: Class Definitions—More Details

This lesson summarizes the major points covered in this chapter.

  • A Boolean-valued method returns either true or false. By defining such methods within a class, we provide a way for a client to test certain conditions relevant to the class.
  • A carefully chosen name for a Boolean-valued method will help us avoid logical errors. Typically, such method names begin with “is” or “has.”
  • A private method generally performs an implementation detail that should be hidden from the client. A private method can be called only from within its own class.
  • We should divide a large task into several smaller subtasks. The public method that implements the large task can then call several private methods, each of which implements one smaller task.
  • Declare a data field as final if its value will be initialized once when an object is constructed and will not change after that time.
  • Declare a data field as static if all objects of the class can share one copy of it.
  • An enumerated data type, or enumeration, lists the values that a variable can have. Define and use an enumeration when we know and can list the possible values for a variable before execution time.
  • An enumeration is actually a class, and so it provides a new data type. It has the methods compareTo, equals, ordinal, toString, and valueOf.
  • A public enumeration should be defined within its own file, just like any other public class. Such an enumeration is available for use within any other class.
  • A private enumeration is defined within a class outside of all method definitions. Such an enumeration is available for use only within the class containing it.
  • The constructor of a class can explicitly invoke another constructor in the same class by using the reserved word this as if it were a method name. This action can occur only within a constructor and must always be its first action.

Get hands-on with 1200+ tech skills courses.