home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lect07c
(non-static) methods
object-oriented programming
Review:
-
The purpose of a constructor is to
.
-
In addition to the parameters, there is one additional (implicit)
variable provided to a constructor:
it is named ,
and it is initialized to .
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:
- To call a static function:
name( arg… )
- To call a (non-static) method:
object.name( arg… )
Your turn:
-
Get an uppercase version of name.
-
What is the 4th letter of name?
-
How to call (a non-static version of) currentForm
for Robot leader = new Robot("43XG-7", "Corvette", false, false )?
-
What is the longer of latestHit and
new Song( "4:33", "John Cage", 4*60+33, false )?
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:
-
Write currentForm as a non-static method.
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:
-
…when there is one (primary) object of a class we're writing,
make it object-oriented
(E.g. Song#fitsOnDisk, Robot#currentShape).
But we sometimes we can't do this, or we want to use still use static methods.
Use a static method when:
-
…when there is no object involved in the function
(e.g. celcToFahr).
-
…when the answer doesn't depend on which object you ask:
E.g. something like Song#getSystemSpeakerVolume
or Robot#unmorphAllRobotsWorldwide
or Song#MB_PER_SEC.
-
…when we can't modify the class-file we want;
then we're reluctantly forced to make a static version of our function.
For example, we will write a method
static int countOccurrences(String s, char target),
and we'll call it via countOccurrences("mississippi", 's').
Even though
we'd might prefer to write this method in an object-oriented way
(calling "mississippi".countOccurrences('s')),
we can't —
we don't have access to the file String.java,
and that's the only way Java would let us add a method to an existing class.
-
(A nuance, relatively rare:)
…when there are (two or more) objects,
but no single object is the most important argument.
E.g. max(Student s1, Student s2) might remain
static, because s1 and s2 are equally
important.
Note that
there are some people who still would choose to
make this object-oriented,
calling it via (say) studA.max(studB),
and not worry about how studA is somehow being privileged
over studB.
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).
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs