![]() |
![]() |
|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
Non-static methods: - how to *call* them
Song s1 = new Song( "Violet Hill", "Coldplay", 180, true );
// Instead of fitsOnDisk(s1,2.5), write:
s1.fitsOnDisk(2.5);
|
/* Instead of:
static boolean fitsOnDisk(Song thisSong, double freeSpace) {
return thisSong.length * MB_PER_SEC <= freeSpace;
}
We will write:
*/
|
We go through our class Song: - remove 'extends Object120' (and making fixes); For each method: - change how we *call* it, to be OO - change how we *define* it, to be OO - make sure our tests still work, before proceeding to the next function
Rule-of-thumb: If the answer to a question depends on who you ask, then the method should be nonstatic.Examples:
Rule-of-thumb: If the answer to a question does not depend on who you ask, then the method should be static.Examples:
Rule-of-thumb: If it makes sense to call a method, even before a single object has been created, then it should (must!) be static.
What's going on: When comparing two object references,== returnstrue if (and only if) they reference (point to) the identically-same object.
(reached here)
What `equals` means: If you don't do anything to say otherwise, your own classes have aequals (inherited fromjava.lang.Object ) which just does the same thing== does (?!):If you want
// Inside class java.lang.Object, deep in the bowels of Java: public boolean equals(Object that) { return this == that; }equals to do something smarter, you have to write it yourself (“overriding the version inherited fromObject ”). That's whatclass String does, which is whyequals works forString s.
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
| ©2012, Ian Barland, Radford University Last modified 2012.Oct.17 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |