setter methods

Also known as mutator methods, setter methods just set the value of a field (often private) in a class.

class TwoDPoint {
    double x;
    double y;
    
    String getAsString() {
      return "(" + this.x + "," + this.y + ")";
    }
    
    void setX(double value) {
      this.x = value;
    }
    
    void setY(double value) {
      this.y = value;
    }
  
}
TwoDPoint origin = new TwoDPoint();
origin.setX(0.0);
origin.setY(0.0);
System.out.println(origin.getAsString());

Previous | Next | Top | Cafe au Lait

Copyright 1997-2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified September 9, 2005