Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using static method vs. object method

Tags:

c#

How often should I use static methods generally? If I have like:

Class1 _class34 = new Class1(parameter);

Class1.DoSomething(_class34, parameter1, parameter2, parameter3, parameter4).

or

_class34.DoSomething(parameter1, parameter2, parameter3, parameter).

I'm having a tendency of calling static method of a class and passing an object of the class like in the first example?

What is the best practice concerning these two examples? Is there any performance, design and general practices things I should pay attention to? Which one should I use generally and which one would you choose like in every day coding scenarios. The first example seems more simple to read (you pass all the parameters and do something), in the second you have to read twice that you are working on an object?

It is not really a big deal, just wondering.

like image 835
iefpw Avatar asked Apr 08 '12 12:04

iefpw


People also ask

When should you use a static method when your method?

You should consider making a method static in Java : 1) If a method doesn't modify the state of the object, or not using any instance variables. 2) You want to call the method without creating an instance of that class.

Why we should not use static method?

Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous. Overriding a static method is not that simple for some languages.

Is static method better?

There is little to no difference between a static method and a non-virtual instance method. The latter just has the this prointer/reference as a "hidden" argument. In the generated machine code, both kinds of calls look very similar. A method should be static if it does not depend on/modify the object.

Are static methods faster Java?

As expected, virtual method calls are the slowest, non-virtual method calls are faster, and static method calls are even faster.


3 Answers

Generally speaking, static methods should only be used when whatever you want to do is independent of any one instance of the class. If you need to directly access or affect the state of a particular instance, a non-static method is usually the way to go.

like image 51
Taymon Avatar answered Oct 20 '22 02:10

Taymon


There's no "more often" answer.
It all depends on the type of usage.

The bottom line is: If an object of the specified class is effected/used, you should always use non-static methods. However, if there's no single instance of the class that is effected/used, you should always use static methods.

In your example you are doing Class1.DoSomething(_class34, parameter1, parameter2, parameter3, parameter4) which is not a good approach, as it strips away all the possibilities Object Oriented programming gives you(such as polymorphism, etc.).

A good example for a scenario where a static function would be needed is factory methods such as String.Parse - which begin without any specific instance of String, yet are connected to the string class.

like image 32
Svarog Avatar answered Oct 20 '22 01:10

Svarog


This is what the runtime already does, every instance method has a hidden 1st argument that passes this. Exposed in the syntax of an extension method.

Explicitly doing the work of the runtime is not particularly useful, the syntax just gets more verbose. And painful given the kind of name you have to come up with. Consider _this instead of _class34 :)

like image 39
Hans Passant Avatar answered Oct 20 '22 03:10

Hans Passant