Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Mr. Happy Object teaches static methods

When to choose static methods over instance methods

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

November 21, 2001

Q 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?

AMany 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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (13)
Login
Forgot your account info?

ThanksBy Anonymous on October 5, 2009, 2:20 amNice article , Thanks

Reply | Read entire comment

ThanksBy Anonymous on September 30, 2009, 12:01 pmvery nice exp.

Reply | Read entire comment

AppreciatedBy Anonymous on August 7, 2009, 6:28 pmShort and simple. I like how you created an object out of everyday expression.

Reply | Read entire comment

Your are realy expert. Why because experts only think and explaiBy Anonymous on July 31, 2009, 6:55 amYour are realy expert. Why because experts only think and explain very natural. Your example is gorgeous and utter natural. Thanks, I realy like the way you are...

Reply | Read entire comment

great explainationBy Anonymous on July 30, 2009, 6:22 amit actually clarified lot of doubts, Thnx

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources