Hello, people! Actually, I am looking for an answer to a question that once put me into a dead end and to which I have not yet found an objective answer, but in precisely what situations would it be necessary to have a closed static class method?
3 answers
Here are some offhand ideas:
- Logger that collects messages only from instances of a particular class
- Just a helper method that is called from another static class method.
- Class instantiation count
- A semaphore or mutex that synchronizes access to class methods (doubtful from an architectural point of view, but still applicable)
The last two points more quickly relate to hidden static fields, not methods, but they can be implemented in the same way. In general, all the points, except for the second, are doubtful, but this is all that I could think of.
Yes, outside this method cannot be used. But the class itself can call this method.
Usually statics are used for some auxiliary algorithms that do not require access to the object data.
But there is one reason. This is not passed to the static method, and theoretically it could be a bit faster than the usual private method.
Static methods can use only static class members. There is no access to the private static method from the outside world. Reminder to the programmer that he can not get to ordinary members of the class Through this method that he can only use in a closed area. In other words, this method is used only for static class members in a closed area. Example of use: An ordinary counter that must exist throughout the program’s work, which can only be changed using this method if the class members are in a closed static area. Accordingly, only the class itself and its friends can use this method.