Summary: Class Definitions—the Fundamentals

In this lesson, we summarize what we covered in this chapter.

  • A class is a plan for creating objects. It contains declarations for the data fields associated with its objects and definitions of methods that implement the objects’ behaviors.

  • The words public and private are examples of access modifiers that indicate where a class, method, or data field can be used. Any class can use something that is public. A private class, method, or data field can be used only by the class that defines it.

  • When creating a class, we should specify its methods and write statements that use them. Only after we are satisfied with our design should we implement the methods.

  • We should make each data field in a class private by beginning its declaration with the access modifier private. Doing so forces a client to use methods to assign values to these fields. In this way, an object’s data is less likely to become corrupt.

  • The first line of the definition of a class, constructor, or method is its header.

  • When formal parameters occur in a method’s header, each one appears with its data type. When we invoke a method, we pass it an argument that corresponds to each formal parameter, but without writing its data type. The same comments apply to constructors.

  • A method that performs an action but does not compute a value, as a result, is known as a void method.

  • A method that returns a value is known as a valued method. It returns a value by executing a return statement.

  • An accessor method, or a get method, or a getter, is a method that returns the value of a data field. By convention, the names of such methods begin with “get.”

  • A mutator method, or a set method, or a setter, is a method that changes the value of a data field. By convention, methods whose names begin with “set” should be mutator methods.

  • A method of class C can have a parameter of type C. Within the method’s implementation, we can access the data fields of the argument that corresponds to this parameter by name.

  • When a formal parameter has a primitive type, such as int, it is initialized to the value of the corresponding argument in the call to the method. This way of passing an argument to a method is known as a call by value. A method cannot change the value of an argument that has a primitive data type.

  • When a formal parameter has a class type, the corresponding argument in the method invocation must be an object of that class type. The formal parameter is initialized to the memory address of that object. The method cannot replace an object that is an argument with another object. However, the method can change the state of such an object if the object is mutable.

  • A constructor is a special method within a class that creates an instance, or object, of the class. The operator new uses a constructor to create a new object of the class.

  • A class can have more than one constructor, with each one differing in its list of parameters. A constructor without parameters is known as the default constructor.

  • If we do not define any constructor in our class, Java will provide a default constructor, that is, a constructor that has no parameters. However, if every constructor that we define has parameters, Java will not define a default constructor for us, so our class will have no default constructor.

  • A local variable is a variable declared within the body of a method’s definition. Its value is available only within the body of the method.

  • The method toString returns a string that represents an object’s data. If we fail to define a method toString, Java will provide its own version. However, Java’s version of toString returns a string that likely will have no meaning to us.

  • Within a method definition, we can write this to represent the method’s receiving object.

Get hands-on with 1200+ tech skills courses.