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

Page 2 of 3

When you declare a method as static, you define that method as being a class method. A class method applies to the class as opposed to any particular instance. The behavior instigated by a class method does not rely on the state of a particular instance. In fact, a static method cannot rely on an instance's state since static methods lack access to this reference. Instead, the behavior of a class method either depends on a state that all objects share at the class level, or is independent of any state at all.

Let's add the following definitions to the original MrHappyObject:

private static int _instantiations;
public MrHappyObject() {
    _instantiations++;
}
public static int instances() { 
    return _instantiations;
}


instances() is a static method. The instances() method returns the number of MrHappyObjects that have been created. The number of instantiations is independent of any particular MrHappyObject. Instead, MrHappyObject tracks the number of instantiations at the class level.

Again, this example is a bit contrived but it demonstrates an important lesson: Class methods instigate behavior that is independent of instance state. instances() returns the number of instances regardless of whether obj1 is happy and obj2 is annoyed. The instance state is simply irrelevant. Any method that is independent of instance state is a candidate for being declared as static.

Note that I say "candidate for being declared as static." Even in the previous example nothing forces you to declare instances() as static. Declaring it as static just makes it more convenient to call since you do not need an instance to call the method. Sometimes you will have methods that don't seem to rely on instance state. You might not want to make these methods static. In fact you'll probably only want to declare them as static if you need to access them without an instance.

Moreover, even though you can declare such a method as static, you might not want to because of the inheritance issues that it interjects into your design. Take a look at "Effective Object-Oriented Design" to see some of the issues that you will face.

Anyway, simply counting the number of instantiations is a fairly useless example -- it's probably not something you will do in your day-to-day coding. However, sometimes you will want to use statics.

Static methods prove useful for creating utility classes and factory methods. (See "Factory Methods" for more information.)

A utility class simply contains static methods and no public constructor. Utility classes are not meant to be instantiated. As Joshua Bloch points out in Effective Java Programming Language Guide, utility classes have earned a bad reputation because they are easily abused and can lead to procedural programming.

However, utility classes do have valid uses. Take a look at java.lang.Math, java.util.Arrays, and java.util.Collections; each is a utility class. These utilities group related methods on primitive values, arrays, and interfaces. These related methods are placed in these utility classes since they cannot go anywhere else (you can't place methods into a primitive, array, or interface after all!).

  • 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