10

I found that there are two type of methods called static methods and instance methods and their differences. But still I couldnt understand the advantages of one over another.

Sometimes i feel that static methods are not 100% object oriented.

Are there any performance differences between this two.

Can someone help?

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
Jaywith.7
  • 1,819
  • 4
  • 15
  • 13

7 Answers7

7

In a perfect OO world there probably wouldn't be any need for static methods (I think Eiffel doesn't have them, either). But at the end of the day what matters is not OO-pureness of your code (C# has enough concepts that aren't strictly pure OO, like extension methods, for example) but rather what you're getting done.

You can use static methods for general helper methods (that don't need a general helper class or state on their own) or things like Color.FromARGB() which behave slightly contructor-like for value types.

In general, any method that doesn't touch an objects state (and therefore is more class-specific than object-specific) can be made static. Performance differences shouldn't really arise. Not very measurable, in any case. Jan Gray's great article Writing faster managed code: Know what things cost has some hard data on this, albeit to be taken with care.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 2
    Even in a "pure OO world", I don't see how making Math.Cos an instance method would be "more OO". For utility methods such as these, there are no conceptual differences between having a static method with N parameters or having an instance method with N-1 parameter where "this" is understood as the first parameter ("Math.Cos(double)" VS "double.Cos()"). – Trillian May 17 '09 at 12:27
6

The usefulness of a static method primarily comes when you need to call the method without ever instantiating the object. For example, maybe the static method is there to actually look up an existing instance and return it (an example being a singleton instance).

As others have stated, you can make any method static if it doesn't access state, and you'll get a tiny performance improvement.

If you actually want to be able to call the method on a specific instance though, and get the benefits of polymorphism (i.e. a derived class can override the behaviour of the method), then you should make the it an instance method.

If your classes implement interfaces, then the methods belonging to those interfaces must also be declared as instance methods.

Dave Cluderay
  • 7,268
  • 1
  • 29
  • 28
6

Instance methods are tight to an instance. So you could see one advantage of static methods is not being tight to an instance. Static methods can (if visible) used by other objects to solve their problems. Sometimes this good and needed. Then you have to think about keeping your static methods in the same class or if you start building utility classes for broader use. I wouldn't see the use of static methods of being "less OO". Static methods is one way to circumvent the shortcomings of OO (especially in single inheritance languages). You can call it a more functional approach (I know it isn't really).

Taking all this is just a bunch of questions that you should ask your code and that should determine if it is better an instance method, a static method of the same class or a static method of another class.

I wouldn't even think about performance issues. It will weaken your design and the difference isn't really that big. Performance is important if you have performance problems.

Norbert Hartl
  • 10,481
  • 5
  • 36
  • 46
2

Instance methods require passing an implicit parameter (the this reference) which make them slightly slower than static methods. But that really should not be the reason to prefer them.

For a related discussion, look at:

Should C# methods that *can* be static be static?

Community
  • 1
  • 1
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 1
    Please the speed argument should only be considered if you are trying to do something either on a very slow platform or hundreds of thounsands of times in one second. My guess is you are working with a >2 Ghz dual core processor. The speed difference between a member function call and a static function call should not be a design criterium. – Harald Scheirich May 17 '09 at 11:08
  • 1
    Yeah, even on much slower platforms, it shouldn't be a design consideration (as I said in the answer). I just noted the performance difference because it's specifically asked. – Mehrdad Afshari May 17 '09 at 11:18
2

If your method uses non-static data members, don't make it static (you "can't").

If your method does not use any non-static data members, you can make it static, but that mostly depends on your design rather than on whether it uses or not uses non-static members (there's not much difference in performance anyway as Mehrdad said).

If you have NO non-static data members in your class, sometimes it's a best practice to make all the methods static (like in the case of grouping helper functions under one class for the sake of good order).

Roee Adler
  • 33,434
  • 32
  • 105
  • 133
1

No one is better than the other. It really depends on your requirement. Class methods are called when you want to apply a change to class as a whole. Whereas Instance methods are called when you are not applying change to the class but to a unique instance (object) of that class.

So I dont see a reason why one should be better than the other.

1

I'm partially guessing based on the heritage of C# but I suspect it's the same as the other OO languages.

Static methods do not require an object to work on. A good example is something like:

Double pi = Math.PI.

Instance methods do require an object. An example is along the lines of:

Integer x = 9;
Integer y = x.sqrt();

Not all information belonging to a class should need an object instantiated for that class to get access to it. All those constants which can be used for creation of objects (Math.PI, Window.OVERLAPPED, etc) are prime examples of this.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953