More on Accessors and mutators


...Section: More on Accessors and mutators

As an object designer, you may want allow users of your object to access and modify (mutate) data.

The wrong way to do this is to make all data public:

class Customer {

  public String custName;
  public int bankBalance;

}

// Later somewhere ....

  Customer c = new Customer ();
  c.bankBalance = -1000;       // Don't want to allow this.
  c.custName = "331-77-9910"   // Oops.
Instead, it's best to allow access only via methods:
class Customer {

  // Internal data.
  private String custName;
  private int bankBalance;

  // Accessors.
  public String getCustName ()
  {
    return custName;
  }  
  public int getBankBalance ()
  {
    return bankBalance;
  }

  // Mutators.
  public void setBankBalance (int newBalance)
  {
    if (newBalance >= 0)
      bankBalance = newBalance;
    else {
      // ... handle error.
    }
  }
  public void setCustName (String newName)
  {
    //... perform extensive checking.
  }

} // End of class "Customer"

// Later somewhere ....

  Customer c = new Customer ();
  c.bankBalance = -1000;       // Won't compile.
  c.custName = "331-77-9910"   // Won't compile.

By convention, accessors are "read-only" and mutators are "write-only".

It is fashionable to create an accessor and mutator for every internal variable. The fact is, many variables don't need accessors or mutators.

Variables should really remain private (or at the very least protected).

Be careful about returning object references, e.g.,

class MasterList {
  Customer[] custList;
  
  // Accessor.
  public Customer getCust (int i)  {
    return custList[i];
  }
}

//... later somewhere...
  MasterList ml;
  //... more stuff...
  Customer c = ml.getCust (9);

  // This is OK.- the real purpose is to 
  // get the name.
  System.out.println (c.getCustName()); 

  // This is not what we had in mind.
  c.setBankBalance (0);

In this case, it's probably better to return just a name:

class MasterList {
  Customer[] custList;
  
  // Accessor.
  public String getCustName (int i)  {
    return custList[i].getCustName();
  }
}


Exercise 6

Write the class Complex:
class Complex {
  // FILL IN YOUR CODE HERE.
}
so it can be tested with:
public class TestComplex {

  public static void main (String[] argv)  {
    Complex c1 = new Complex();
    c1.setValue (1, 1);

    System.out.println ("c1's real part = " + c1.getRealPart()
                        + "\n" +
                        "c1's imag part = " + c1.getImagPart());
    System.out.println ("|c1| = " + c1.getMagnitude());

    Complex c2 = new Complex();
    c2.setValue (6, 8);

    System.out.println ("c2's real part = " + c2.getRealPart()
                        + "\n" +
                        "c2's imag part = " + c2.getImagPart());
    System.out.println ("|c2| = " + c2.getMagnitude());
  }

} // End of class "TestComplex"
To invoke the square root function, use Math.sqrt, as in
       double sqrtOf2 = Math.sqrt (2);


Here's a hint:


rhyspj@gwu.edu