Why and when should I make a class 'static'? What is the purpose of 'static' keyword on classes?

  • The static keyword on a member in many languages mean that you shouldn't create an instance of that class to be able to have access to that member. However, I don't see any justification to make an entire class static. Why and when should I make a class static?

    What benefits do I get from making a class static? I mean, after declaring a static class, one should still declare all members which he/she wants to have access to without instantiation, as static too.

    This means that for example, Math class could be declared normal (not static), without affecting how developers code. In other words, making a class static or normal is kind of transparent to developers.

    If you are coding in Func Prog style a la Rich Hickey, this can be useful.

    In my opinion, static classes are merely a crutch via which C# attempts to hide a massive design flaw in the language. Why structure a language around this nonsensical everything-has-to-be-a-class ideology, only to then introduce additional syntax so you can cheat and work around this needless restriction. If C# had just allowed free functions from the get-go, there would be no need for static classes.

  • It makes it obvious to users how the class is used. For instance, it would be complete nonsense to write the following code:

    Math m = new Math();

    C# doesn’t have to forbid this but since it serves no purpose, might as well tell the user that. Certain people (including me) adhere to the philosophy that programming languages (and APIs …) should be as restrictive as possible to make them hard to use wrong: the only allowed operations are then those that are meaningful and (hopefully) correct.

    I disagree with that philosophy. The problem I have is that needs change. So while it may have made sense to restrict it before now that restriction hinders our development for future needs. And the legacy library we are forced to use to interface with the old system (The one you wrote and sealed and staticed all of the classes) does not provide any way for us to extend or modify that code you wrote. Just because you had no reason to instantiate your class does not mean that I will not have a need for it in the future. While staticing a utility class makes sense, consider the future too.

    @Chad Then the library is badly designed. Extending classes that were not designed for extension ultimately doesn’t work, so best make the class `sealed` in the first place. I acknowledge that not all people agree with this sentiment but the comments aren’t a good place to discuss this. But in the case of `static` this problem doesn’t even pose itself: instantiating `System.Math` will **never** make sense.

    @Konard, if you declare all the members of the `Math` class as static without declaring the `Math` class as static, still you can use it, without need for instantiation.

    @Saeed - This is true, but you could also instantiate the class, which doesn't serve any purpose. What do I do with an _instance_ of the Math class? To explicitly show the intent of the class (no instantiation needed or wanted), it's marked `static`.

    @Saeed How does this relate to my answer?

    @Konrad, -1 because it's obvious. Read the question carefully. If you define all the methods of a non-static Math class as static, then you don't need to write `Math math = new Math()`, while the class is not static itself.

    @Saeed Let’s turn this around: I propose you read *my answer* again since you seem to have misunderstood it: Your comments are unrelated to my answer. I never said that you need to write `new Math()`.

    Yeah @Konrad, you're right. I removed my down-vote (say's that you can't). But, your explanation isn't kind'of straight man :). Anyway, you mean that the only purpose of `static` keyword on classes is to prevent instantiation code lines?

    I think the problem was with new Math. They never should have started teaching it. Or gone off the Gold Standard...

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM