6

I have to ask this question because I feel that only experienced programmers can know the pros and cos of static members in a class. I've read books explaining about static members, I've also used many static members in my project according to my points of view.

As I understand, if there is some class which is used only 1 time in my project, I mean there is no need to create some or many instances, I should make all its members static, especially static methods. Is this true? This has another benefit because of that calling to static members can be done easily without creating new instances or passing instances between our classes.

Using static members in my projects doesn't show me what's wrong with it, my project seems to run normally, of course I don't mean I like using static members and use it randomly, frequently (as I explained my point of view above). I think there may be some pros and cons of static members (that I don't know) and I would like to know from your experience. Please share with me.
Thank you!

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
King King
  • 61,710
  • 16
  • 105
  • 130
  • 1
    You should ask the question the other way: When *should* I use static members? In general, don't use statics unless you have to. – Jim Mischel Apr 18 '13 at 23:25
  • My question focused on finding anything which is not good when using statics, not focused on finding anything which is good when using statics. And the same idea, I don't use statics in general, but I have to use it in some case to keep my code clean, even in some cases I don't know what name I should use for my class if intending to use it as instances, so I just call it any name like 'General', 'Misc', 'Tools',... and make all its members static. Thanks. – King King Apr 19 '13 at 14:50
  • @KingKing ~"some class which is used only 1 time in my project". Sounds like you need a singleton! – IgorGanapolsky Jul 10 '14 at 20:00

4 Answers4

5

Check out the following posts:

As I understand, if there is some class which is used only 1 time in my project, I mean there is no need to create some or many instances, I should make all its members static, especially static methods. Is this true?

It depends. I believe that static classes are good if you want to access them globally throughout your application (ie, utility classes, helper functions, etc).

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

Keep in mind that declaring a class static allows it to stay in memory for the lifetime of the application. This means if the static class is only used once, it will still remain in memory even after it is not needed/will be used again. Instead, if the class is only used once, you may be better off creating a regular class instance so that GC will clean up after you're done using it.

openshac
  • 4,966
  • 5
  • 46
  • 77
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • When I intend to reuse my classes in other projects, I would not use statics, the most cases I use statics are when I intend to use them locally in a particular project (not for reuse), make some methods global and easy to access in the whole project, what I care is if there is some performance or memory issue with static members, in fact I've used static methods more than static variables, and in that case the memory issue doesn't really matter, does it? Thanks for sharing the good links. – King King Apr 19 '13 at 14:32
4

As I understand, if there is some class which is used only 1 time in my project, I mean there is no need to create some or many instances, I should make all its members static, especially static methods. Is this true?

It can be - there are times when you want an instance and it can't be static because it implements an interface for example - in which case you'd use the singleton pattern.

Static methods can be useful and if your class doesn't contain state then the likelihood is should be fine as a static class.

NDJ
  • 5,189
  • 1
  • 18
  • 27
  • 1
    Yes, saving states should be instantiated than declaring static members, however I don't like using static variables which contain a large memory, I just care about using static methods which are some kind of utilities for handling data ... applying in a global scope, if using instances, I have to declare quite lot of instances in my classes which will make my code complex, and I have also noticed of multi-threaded tasks which require using statics carefully. Thanks for your sharing. – King King Apr 19 '13 at 14:37
  • @KingKing Please consider using periods to break your comment into sentences. People who read your posts here will have a difficult time understanding what you are trying to say. – IgorGanapolsky Jul 10 '14 at 20:07
3

My main problem with statics is that it makes it really hard to do polymorphism (replace one thing with another at run time). The place you often want to do this is when writing tests, I want to replace the FileManager class with a MockFileManager. If I'm using a non-static class, I just use interfaces to achieve polymorphism, and hopefully I'm using IoC pattern and I can pass in my mock implementation in place of the real one (yay polymorphism!).

However, if my FileManager class is all static, it makes it really hard to replace it with something else dynamically.

Note: There are Code Analysis rules which tell you to make things static for perf reasons. I turn those rules off.

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
  • I think once we decided to use statics, we treated it as some kind of utilities (like P/Invoke functions) which require some objects passed in and process and give out the result, it's for static methods, for static variables, I'm not really interested in using them except it's really needed. Thanks. – King King Apr 19 '13 at 14:45
  • 1
    Right, but if you use an actual object instead of static methods, you have the opportunity to swap it out with a Mock object at run-time, say for unit testing. Sometimes I may want to only test a specific class, not all the helper methods it uses. Especially if some of those helper methods touch expensive resources such as file system, network, DB, etc. I guess the main point is, that everything you can do with a static method you can do with an instance method and *more*. And if you use IoC there's no real burden to creating instances since they just get injected in by the container. – Dylan Smith Apr 19 '13 at 15:31
  • @DylanSmith Which Code Analysis rules tell you to make things static? I've never seen it. – IgorGanapolsky Jul 10 '14 at 20:11
  • CA1822 Mark Members as static: http://msdn.microsoft.com/en-us/library/ms245046.aspx – Dylan Smith Jul 10 '14 at 20:24
2

To quote someone who can explain it way better than me :

The abuse of static classes can be considered bad practice. But so can the abuse of any language feature.

I make no distinction between a non-static class with only static methods and a static class. They are effectively the same thing, except that static classes allow the compiler to enforce the developers intent (no instantiating this class, convenient syntax for accessing its functionality, etc).

A proliferation of "Helper" classes can get you into trouble (design, maintainability, readability, discoverability, other-abilities...). No argument here. But can you argue that a "Helper" class is never appropriate? I doubt it.

Indeed, responsible use of static classes can have great benefits to your code:

The Enumerable static class provides a set of extension methods most of us have come to love. They are a logical set of functionality / business logic that isn't related a instance of any particular type. Services provided by the environment/context: eg Logging, Configuration (sometimes) Others (that I can't think of at the moment :)) So no, in general its not bad practice. Just use them wisely...

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

Rule of thumb ? If you know why you should use static and can explain it then you should use it.

Community
  • 1
  • 1
phadaphunk
  • 12,785
  • 15
  • 73
  • 107