In which case should not use static elements in the class?

I have to ask this question because I feel that only experienced programmers can learn about the pros and cons of static members in a class. I read books about static members, I also used a lot of static members in my project according to my points of view.

As I understand it, if there is some class that is used only once in my project, I mean that there is no need to create several or several instances, I have to make all its elements static, especially static. It's true? This has another advantage because calling static members can be easily done without creating new instances or passing instances between our classes.

Using static elements in my projects does not show me what is wrong with it, my project seems to work fine, of course, I don't mean that I like to use static members and use it randomly, often (as I explained my point of view above). I think there may be some pros and cons to static members (which I donโ€™t know), and I would like to know from your experience. Please share with me.
Thanks!

+4
source share
4 answers

Send the following messages:

As I understand it, if there is some class that is used only once in my project, I mean that there is no need to create several or several instances, I have to make all its elements static, especially static. It's true?

It depends. I believe that static classes are good if you want to access them all over the world throughout the application (for example, utility classes, helper functions, etc.).

Use a static class to contain methods not related to a specific object. For example, the general requirement is to create a set of methods that do not affect instance data and are not associated with a specific object in your code. You can use a static class to hold these methods.

Keep in mind that declaring a static class allows it to remain in memory for the entire life of the application. This means that if a static class is used only once, it will still remain in memory even after it is not needed / will be used again. Instead, if the class is used only once, you might be better off creating a regular instance of the class so that the GC clears it after using it.

+4
source

As I understand it, if there is some class that is used only once in my project, I mean that there is no need to create several or several instances, I have to make all its elements static, especially static. It's true?

It can be - there are times when you need an instance, and it cannot be static, because it implements the interface, for example, in this case you should use the singleton pattern.

Static methods can be useful, and if your class is stateless, the likelihood of this being edits should be as beautiful as a static class.

+4
source

My main problem with statics is that it is very difficult to perform polymorphism (at runtime, replace one thing with another). The place you often want to do is when writing tests, I want to replace the FileManager class with MockFileManager. If I use a non-static class, I just use interfaces to achieve polymorphism, and I hope I use the IoC pattern and I can pass my mock implementation instead of the real one (yay polymorphism!).

However, if my FileManager class is all static, it is very difficult for him to replace it with something else dynamically.

Note. There are code analysis rules that tell you to make things static for primary reasons. I will disable these rules.

+3
source

To quote someone who can explain this better than me :

Abusing static classes can be considered bad practice. But this may be the abuse of any feature of the language.

I do not distinguish between a non-static class with only static methods and a static class. They are practically the same, except that static classes allow the compiler to force the developers to fulfill their intentions (without creating an instance of this class, convenient syntax for accessing its functionality, etc.).

Distributing Helper classes can cause you problems (design, maintainability, readability, openness, other features ...). There are no arguments. But can you say that the Helper class never fits? I doubt it.

Indeed, the responsible use of static classes can have big advantages for your code:

The variable static class provides a set of extension methods that most of us love. This is a logical set of functionality / business logic that is not associated with an instance of any particular type. Services provided by the environment / context: for example, registration, configuration (sometimes) Other (which I canโ€™t think about at the moment :)) So no, in general, it's a good practice. Just use them wisely ...

As for me, I use them when I need to use them, it doesn't really matter if this seems like bad practice or not.

The rule of the thumb? If you know why you should use static and can explain it, you should use it.

+2
source

All Articles