2.7. Parts of a Java Class

A Java class defines what objects of the class know (fields) and can do (methods). The class also defines how to initialize the fields when the object is first created (constructors).

2.7.1. Fields - Instance Variables

Fields hold the data for an object. Fields record what an object needs to know to do work in the program. Fields are also called instance variables or object variables or properties.

All fields on the AP CS A exam should be declared private. Think of private as like your diary. Only you should have direct access to it. In this case private means that only the code in this class can directly access the field values.

Note

Fields are declared right after the class declaration. They start with private then the type of the field and then a name for the field.

The Person class declares two fields: name and cell. Name is the person’s name and cell is their cell phone number. These are both things that you might need to know about a person.

/// fields ////////////////
private String name;
private String cell;
Click on all the field declarations in the following classRemember, fields are private and are declared after the class declaration.
public class Name {

    private String first;
    private String last;

    public Name(String theFirst, String theLast) {
        first = theFirst;
        last = theLast;
     }

     public void setFirst(String theFirst) {
        first = theFirst;
     }

     public void setLast(String theLast) {
        first = theLast;
     }

}

2.7.2. Constructors

Constructors don’t actually construct the object. The class makes the object and then executes a constructor to initialize the values of the fields (instance variables). You will only work with public constructors on the exam.

Note

Constructors are specified after the fields and before any methods. They typically start with public and then the name of the class. They can take data (specified in parentheses) which is used to initialize the fields.

The Person class has one constructor that takes two values: a string that is the name and a string that is the cell phone number. To find a constructor in a class look for something with the same name as the class and no return type.

/////// constructors ////////////////////
public Person(String theName, String theCell)
{
  this.name = theName;
  this.cell = theCell;
}
Click on all the parts of the contsructorConstructors are public and have the same name as the class.
public class Name {

    private String first;
    private String last;

    public Name(String theFirst, String theLast) {
        first = theFirst;
        last = theLast;
     }

     public void setFirst(String theFirst) {
        first = theFirst;
     }

     public void setLast(String theLast) {
        first = theLast;
     }

}
    2-7-1: What best describes the purpose of a class’s constructor?
  • Determines the amount of space needed for an object and creates the object
  • The object is already created before the constructor is called so there would be no need for this in the constructor.
  • Names the new object
  • Constructors do not name the object.
  • Return to free storage all the memory used by this instance of the class.
  • Constructors do not free any memory. In Java the freeing of memory is done when the object is no longer referenced.
  • Initialize the fields in the object
  • A constructor merely initializes the fields to their default values or in the case of a parameterized constructor, to the values passed in to the constructor.

2.7.3. Methods

Methods define what an object can do or the behavior of the object.

Most methods you work with on the exam will be public.

Note

Methods define what the object can do. Methods are specified after the constructors. They typically start with public then a type, then the name of the method. They can take data as input which is specified in parentheses.

The Person class has methods for getting the name and cell phone and for setting the cell phone. Methods that get information from an object are called getters or accessors. Methods that set field values are called setters or mutators.

//////////// methods ///////////////////////
public String getName()
{
   return this.name;
}
public void setName(String theName)
{
   this.name = theName;
}

public String getCell()
{
   return this.cell;
}

public void setCell(String theCell)
{
   this.cell = theCell;
}

public String toString() { return "name: " + this.name + ",
                           cell: " + this.cell; }
Click on all the parts of the methods in the following classMethods follow the constructor. They include a return type in case they returns something from the method.
public class Name {

    private String first;
    private String last;

    public Name(String theFirst, String theLast) {
        first = theFirst;
        last = theLast;
     }

     public void setFirst(String theFirst) {
        first = theFirst;
     }

     public void setLast(String theLast) {
        first = theLast;
     }

}
Next Section - 2.8. Intro to Java - Summary