Skip to main content

(Not So) Stupid Questions: (1) Static Methods

December 30, 2003


(Not So) Stupid Questions:

Static Methods





Editor's note: Sometimes the most interesting discussions begin when someone says, "This may be a stupid question, but ...". If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. The uncertainty points out an ambiguity in the specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will print one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section.

Remember that new people are joining the Java community all the time and may be looking for help from those with more experience. Also, those who began with Java as their first language can benefit from those coming to the community with experience in other languages. As always, answer the questions with kindness. You are also welcome to submit your questions to

This may be a stupid question, but ... "Should I try to declare more of my methods to be static?"

I was recently reading "Item 8: Always override hashCode when you override equals" in Joshua Bloch's book Effective Java. Notice that the rangeCheck() method is static in the following code listing from that item.

public final class PhoneNumber {
  private final short areaCode;
  private final short exchange;
  private final short extension;

  public PhoneNumber(int areaCode, int exchange, int extension) {
    rangeCheck(areaCode,    999, "area code");
    rangeCheck(exchange,    999, "exchange");
    rangeCheck(extension,  9999, "extension");
    this.areaCode  = (short) areaCode;
    this.exchange  = (short) exchange;
    this.extension = (short) extension;
  }

  private static void rangeCheck( int arg, int max, String name) {
    if (arg < 0 || arg > max )
      throw new IllegalArgumentException(name +": " + arg);
  }

  public boolean equals(Object o) {
    if (o== this)
      return true;
    if (!(o instanceof PhoneNumber))
      return false;
    PhoneNumber pn = (PhoneNumber)o;
    return pn.extension == extension &&
           pn.exchange  == exchange &&
           pn.areaCode  == areaCode;
  }

  // Remainder omitted

}

The rangeCheck() method does not depend on any instance variables, so the method could have been declared static or not. On the other hand, the equals() method depends on the values of the extension, exchange, and areaCode variables. The equals() method could not be static unless those three variables were all static, and then you would have one single phone number shared by all instances of the PhoneNumber class.

So I think it is clear when a method can not be static. My question is,

When a method can go either way, when should you make it static and when should it belong to the object?

It seems that, at least when we teach Java newcomers, we do not encourage them to consider static methods (other than main()), and we introduce the concept with static variables that are often used as constants.

(Not So) Stupid Questions is where we feature the questions you want to ask but aren't sure how.

Related Topics >> Programming   |