|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
November 21, 2001
When would you create static methods as opposed to instance methods? I understand that static methods allow you to use those
methods without having to create an instance of that class, and that class methods apply to the class rather than an object.
Are these the only reasons? Could you give an example of a case where you would use a class method over an instance method?
Many Java developers find it confusing to decide when, and when not to declare a method as static. However, making the choice
is simple if you have a clear understanding of the difference between a class method and an instance method.
Note: You can download the source code that accompanies this article from Resources.
Consider the following class definition:
public class MrHappyObject {
private String _mood = _HAPPY;
private final static String _HAPPY = "happy";
private final static String _ANNOYED = "annoyed";
private final static String _ANGRY = "angry";
public void printMood() {
System.out.println( "I am " + _mood );
}
public void receivePinch() {
if( _mood.equals( _HAPPY ) ) {
_mood = _ANNOYED;
} else {
_mood = _ANGRY;
}
}
public void receiveHug() {
if( _mood.equals( _ANGRY ) ) {
_mood = _ANNOYED;
} else {
_mood = _HAPPY;
}
}
}

Figure 1. Mr. Happy Object
First, before I get emails about it, there are more object-oriented ways to track and transition between states. However, those fancy ways would detract from the intent of the example. Now, without further ado...
printMood(), receivePinch(), and receiveHug() are all instance methods. Syntactically, you call these methods instance methods because they are not static; but the important
distinction concerns why I didn't declare them as static.
Instance methods are instance methods because they rely on the state of the specific object instance. Instance methods are tied to a particular instance because the behavior that the method invokes relies upon the state of that particular instance.
Consider the following example:
MrHappyObject obj1 = new MrHappyObject(); MrHappyObject obj2 = new MrHappyObject(); obj1.printMood(); obj2.printMood();
When obj1 and obj2 first instantiate, they have the same state -- they are born happy. As a result, when the printMood() is called on each instance, each object prints "I am happy" to the screen. However, every object instance has its own state that can vary independently of all other instances of that
class of object.
As obj1 and obj2 go through their day, their states can vary independently from one another. Here I hug obj1 and pinch obj2:
obj1.receiveHug(); obj2.receivePinch(); obj1.printMood(); obj2.printMood();
Now when I query each instance's mood, obj1 is happy while obj2 is annoyed. While the example is silly, it drives home a point: Every instance possesses its own state, and that state is
independent of every other object. Any method whose behavior depends upon the particular state of a particular instance is
an instance method. You should not declare such a method as static.