A static class is a way to make a class which has nothing in common with OOP. Essentially, this is a package of members which are completely equivalent to a set of static variables, constants and methods (well properties are added) as they are understood before adventure of OOP. This effect can be obtained by having all members of the class static. Another extra feature is added access modifiers. Making class static by using key word
static makes
no functional difference at all.
So, why using key word
static for a class?
This is just a feature of support of programming activity, a device used to protect us from accidental programming mistakes. If we do it,
it prevents us from:
- Accidentally adding an instance member to the class;
- Accidental instantiation of the class when creating of the instance would not make sense.
- Using a class as a generic type parameter with the type constraint
new
.
When we try to do either of these mistakes, the code will not compile.
Pure static classes (that is, all classes with the key word
static
) has more or less limited application. At the same time, they are very useful in certain cases. A typical use of such class is a
utility class, a collection of some utility methods working only through explicit parameters. Some method are done private, other methods can call them or each other, but functionally all methods are independent.
—SA