views:

685

answers:

7

In addition, are there any performance advantages to static methods over instance methods?

I came across the following recently: http://www.cafeaulait.org/course/week4/22.html :

When should a method be static?

  1. Neither reads from nor writes to instance fields
  2. Independent of the state of the object
  3. Mathematical methods that accept arguments, apply an algorithm to those arguments, and return a value
  4. Factory methods that serve in lieu of constructors

I would be very interested in the feedback of the Stack Overflow community on this.

A: 

Here is a related discussion as to why String.Format is static that will highlight some reasons.

Jason Bunting
+8  A: 

Make methods static when they are not part of the instance. Don't sweat the micro-optimisations.

You might find you have lots of private methods that could be static but you always call from instance methods (or each other). In that case it doesn't really matter that much. However, if you want to actually be able to test your code, and perhaps use it from elsewhere, you might want to consider making those static methods in a different, non-instantiable class.

Tom Hawtin - tackline
+2  A: 

Whether or not a method is static is more of a design consideration than one of efficiency. A static method belongs to a class, where a non-static method belongs to an object. If you had a Math class, you might have a few static methods to deal with addition and subtraction because these are concepts associated with Math. However, if you had a Car class, you might have a few non-static methods to change gears and steer, because those are associated with a specific car, and not the concept of cars in general.

Kyle Cronin
+1  A: 

Performance-wise, a C++ static method can be slightly faster than a non-virtual instance method, as there's no need for a 'this' pointer to get passed to the method. In turn, both will be faster than virtual methods as there's no VMT lookup needed.

But, it's likely to be right down in the noise - particularly for languages which allow unnecessary parameter passing to be optimized out.

Roddy
Right down in the completely optimised away by the compiler when it matters (most of the time).
Tom Hawtin - tackline
It can't be optimized away in C++. The caller has no way of knowing that the callee isn't going to use "this", so must pass it in regardless. For other languages, YMMV.
Roddy
+1  A: 

@jagmal I think you've got some wires crossed somewhere - all the examples you list are clearly not static methods.

Static methods should deal entirely with abstract properties and concepts of a class - they should in no way relate to instance specific attributes (and most compilers will yell if they do).

For the car example, speed, kms driven are clearly attribute related. Gear shifting and speed calculation, when considered at the car level, are attribute dependent - but consider a carModel class that inherits from car: at this point theyy could become static methods, as the required attributes (such as wheel diameter) could be defined as constants at that level.

iAn
You are right. I really had got things really mixed up. May be because I was sleep deprived during those days. In the spirit of SO, I have deleted the comment and I dont think I have anything more to add than what people have already told. Thanks anyways.
Jagmal
+1  A: 

Just remember that whenever you are writing a static method, you are writing an inflexible method that cannot have it's behavior modified very easily.

You are writing procedural code, so if it makes sense to be procedural, then do it. If not, it should probably be an instance method.

This idea is taken from an article by Steve Yegge, which I think is an interesting and useful read.

Mike Stone
+4  A: 

Another problem with static methods is that is a quite painful write unit test to them - in Java, at least. You can't mock a static method in any way. There is a post on google testing blog about this issue. My rule of thumb is write static method when they haven't external dependencies (like database access, read files, emails and so on) what helps to keep them as simple as possible.

Kind Regards

marcospereira