12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

78 chapter 42. Compile and execute this program and check that the output agrees withthe results you obtained in the exercises in §4.2.Nonstatic methods go about their business by modifying the properties of theobjects to which they are attached (complex numbers in the present case). In fact, weshall see that nonstatic methods are literally appended to the names of objects muchas the endings of verbs are modified when their tenses change. In a sense, whenthe method is appended to an object, it creates a new object. To cite an instance, online 28 we see the operationc.add(b); // Nonstatic addition 28Study the way this statement says to take the complex number object c and modifyit using the add method that adds b to the object. This statement results in newvalues for the parts of object c. Because object c is modified by this action, line 28is equivalent to our static operationc = add(c,b); // Static method equivalent 28Regardless of the approach, since c now contains the sum c+b, if we want to usec again, we must redefine it, as we do on line 31. On line 32 we take object c andmultiply it by b viac.mult(b); // Nonstatic multiplication 32This method changes c to c*b. Thus line 32 has the static method equivalencec = mult(c, b); // Static method equivalent 32We see from these two examples that nonstatic methods are called using the samedot operator used to refer to instance variables of an object. In contrast, static methodstake the object as arguments and do not use the dot operator. Thus we calledthe static methods via add(c,b) and mult(c,b) and called the nonstatic methods viac.add(b) and c.mult(b).The static methods here do not need a dot operator since they are called fromwithin the class Complex or ComplexDyn that defined them. However, they wouldneed a dot operator if called from another class. For example, you may have seenthat the square root method is called Math.sqrt(x). This is actually the static methodsqrt from the class Math. You could call the static add(c,b) method of class Complexby using Complex.add(c,b). This works within the program (class) Complex as wellbut is not required. It is required, however, if add is called from other classes.Observe now how the object-oriented add method has the distinctive formpublic void add(ComplexDyn other) 10{this.re = this.re + other.re; this.im = this.im + other.im;} 11Line 10 tells us that the method add is nonstatic (the word static is absent), that novalue or object is returned (the void), and that there is one argument other of the type−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 78

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!