RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

lect07c
(non-static) methods
object-oriented programming

Review:

Calling a non-static method

String name = "Hillary Clinton"

// calling a static function:
Object120.substring(name, 9, 13)
Object120.length(name)

// calling a non-static "method":
name.substring(9,13)
name.length()
Check out the documentation for class String.

This also holds true for our own (non-static) functions: instead of

Song latestHit = new Song( "U Smile", "Justin Bieber", 3*60+17, true );

Song.fitsOnDisk(latestHit, 3.1)
what would we write instead?

Syntax:

Your turn:

Writing a non-static method

Our previous, static function:

    static boolean fitsOnDisk( Song aSong, double freeSpace ) {
        if (aSong.length/60.0 <= freeSpace) {
             return true;
         }
         else {
            return false;
         }
    }
If re-written in as a non-static (object-oriented) method, we have:
    static boolean fitsOnDisk( Song aSong, double freeSpace ) {
        if (aSongthis.length/60.0 <= freeSpace) {
             return true;
         }
         else {
            return false;
         }
    }
Instead of taking in a Song as the first parameter, there is an additional (implicit) parameter named this, which is initialized to “the song on which the fitsOnDisk method was called”.

Your turn:

Choosing static vs non-static methods

In Java, we'll try to make a method non-static when we can. Use a non-static method when:

But we sometimes we can't do this, or we want to use still use static methods. Use a static method when:

What does non-static buy us?

WTF? The differences between static and non-static methods seem purely mechanical -- I can take any static method (whose first argument is an object) into a non-static method, and vice-versa. So why even have non-static methods? Well, we won't really talk about “inheritance” until the last week of the this class, but here's a teaser:

Object-oriented programming lets you write code which is compatible with other code that will be written in the future!

For example, consider a silly function:

  static void printMyPreference( Object obj1, Object obj2 ) {
    System.out.println( "I prefer " + obj1.toString() + " to " + obj2.toString() );
    // Just ask obj1 and obj2 to give me their `toString` results.
  }
This function can take in any object of type Object. It turns out, in Java every class extends Object! So I can actually pass printMyPreference a Song and a Robot, and it would work — even though printMyPreference may have been written years before you ever thought about creating class Robot.
And it gets better: If Robot does not contain a method toString, then printMyPreference will call some pre-written version which works for all Objects. But if I suddenly add public toString() { return "Robotz Rool!"; } to my class Robot, then when printMyPreference asks “hey obj1, give me your toString result”, the Robot will know to call this brand new toString method — even though I only wrote the method after printMyPreference was written!

We won't say much more about these things now, but in the last week of class we'll talk about these things — inheritance (using “extends”) and “polymorphism” (writing a function which takens in, say, any Object without knowing what particular “subclass” of Object it will be handed when called).

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Nov.17 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme