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.
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.
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.
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.
As expected, virtual method calls are the slowest, non-virtual method calls are faster, and static method calls are even faster.
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.
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With