While developing an application in OOP, should static methods be avoided as much as possible?
Upvotes: 1
Views: 252
Reputation: 888293
No, they shouldn't.
Any method that does not operate on an instance of an object should be static
.
There is no point in requiring an object instance if you don't need it.
Upvotes: 1
Reputation: 19064
There are very specific cases that prescribe their use such as passing a class method to a C API that needs a function pointer or a means to create certain patterns such as Singleton. Generally you don't want to use one unless there is a good reason.
They are otherwise discouraged because their use beyond some limited cases implies you have global data present since they don't have automatic access to the 'this' pointer. That violates OO principles such as data hiding.
Upvotes: 1
Reputation: 134275
It depends - a static method is necessary in a OOP language such as Java or C# if you really just need to write a stand-alone function. In this case you might create some sort of Utility
class that contains various static methods.
Upvotes: 0
Reputation: 56123
In my opinion, static methods are simpler than instance methods, and therefore they should be used as much as possible.
The thing is to know when it's not possible: which is usually, i.e. whenever the method needs to read or write object/instance data members.
Upvotes: 0