Methods

Methods say what an object does.

class TwoDPoint {
    double x;
    double y;
    
    void print() {
      System.out.println(this.x + "," + this.y);
    }
    
}

Notice that you use the Java keyword this to reference a field from inside the same class.

TwoDPoint origin = new TwoDPoint();
origin.x = 0.0;
origin.y = 0.0;
origin.print();

noun-verb instead of verb-noun; that is subject-verb instead of verb-direct object.

subject-verb-direct object(s) is also possible.


Previous | Next | Top | Cafe au Lait

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