Skip to content

Getters and Setters

A get method provides access to a field.

public double getBalance() {
  return balance;
}
* We call this a getter because it gets data from an object.

A set method modifies the value of a field.

public void setBalance(double bal) {
  balance = bal;
}

We add these to objects as we see fit.

  • Maybe we do not want a user to set the balance directly; we will not create a setter method in our class.

  • If data is very sensitive, like a password, we will not supply a getter or setter.

Conventions

Getter and setter methods have conventions for how we define them.

  • The method names are always getPropertyName and setPropertyName.

  • PropertyName is capitalized with camel case.

PropertyName is usually the name of a field, but it doesn't have to be.

  • Getters do not take parameters, and return a property value.
public double getBalance() {
  return balance;
}
  • For properties of type boolean the convention for a getter is to use isPropertyName instead of getPropertyName.

    private boolean overdrawn;
    
    public boolean isOverdrawn() {
      return overdrawn;
    }
    
  • Setters have a return type of void, take one argument, and set the value of a property.

public void setBalance(double bal) {
  balance = bal;
}

Practice Exercise

A "property" of an object is how an object can determine another object's state. What part of a class would a property be?

If a field is private, other classes can't see it, so the field can't be the property.

But if all classes follow the getter and setter convention, the "property" would be PropertyName after get or set, regardless of the field name.

So getters and setters define the "properties" of an object's field.


Prev -- Up -- Next