Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is better? Static methods OR Instance methods

Tags:

c#

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?

like image 402
Jaywith.7 Avatar asked May 17 '09 10:05

Jaywith.7


3 Answers

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.

like image 196
Joey Avatar answered Oct 29 '22 23:10

Joey


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.

like image 45
Dave Cluderay Avatar answered Oct 29 '22 21:10

Dave Cluderay


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.

like image 21
Norbert Hartl Avatar answered Oct 29 '22 23:10

Norbert Hartl