Introduction: Class Definitions—More Details

We begin this chapter with its prerequisites, learning objectives, and overview.

We'll cover the following

Prerequisites

Objectives

After completing this chapter, you should be able to:

  • Define Boolean-valued methods within a class definition
  • Define and use private methods within a class definition
  • Use data fields that are either final or both static and final when appropriate
  • Define and use enumerations
  • Write constructors that invoke another constructor within the same class

Overview

Our discussion of class definitions began in the chapter Class Definitions—The Fundamentals. This chapter continues that introduction by first considering two categories of methods: Boolean and private. Boolean-valued methods are simply methods that return a value of either true or false. A class has such methods so that a client can test for certain conditions. A private method is one that another method definition invokes as a “helping method.” That is, the private method does a portion of the task of the public method. Private methods cannot be invoked by the client of the class. They are a part of the class’s implementation, and so are hidden from public view.

This chapter also introduces enumerations. An enumeration defines a data type that represents a collection of known, discrete values, such as a limited set of numbers or letters. We can define an enumeration instead of defining a group of named constants. Java will then ensure that a variable of this new data type does not have a value outside of the enumeration. Finally, we look at how one constructor can call another.

Get hands-on with 1200+ tech skills courses.