Why can static classes have only static elements?

As the name implies, I would like to understand why static classes can only have static members. Hope it will be the same in all programming languages. Therefore, I think the explanation will be the same for all languages.

+7
oop static class class-design
source share
6 answers

Static classes cannot be created, so it will not have any instance. Non-static members will need an instance of their class to access. Since a static class cannot have an instance, you cannot access non-stationary elements, if any.

Therefore, static classes can only have static members.

+14
source share

This is not a design decision, but a logical one. The simplest start is to look at the corresponding definitions of concepts:

A static class is one that cannot be created. This means that you cannot create objects of this type of class.

Non-static members are bound to a specific instance of the class. They contain data that is associated exclusively with one object of this type of class.

So, if a static class contains non-static members, you can never access this data or call this method, because you can never instantiate an object of this type of static class. Instead, you should have all the static members that can be called directly from a static instance of the class.

However, you can have non-static classes containing static elements. Thus, you can access the data or call methods exposed as static elements without creating an instance of the object of this class. However, you can also create an object of this type of class and access non-stationary (or instances) elements. For example, if you have a Circle class, you may have static members, such as the CalculateArea function and the PI field. These members apply to all circles, as a rule, only because they are circles. But you can also have non-static members associated with specific instances of this class, because they describe specific circle objects. These can be the Diameter and Circumference . You can also have non-static functions that calculate the area of ​​a circle, given the data stored in non-static fields for that particular instance.

+7
source share

A static class cannot be created. Therefore, non-static members could never be accessed.

If you want to mix and match static members, do not put the static class.

+4
source share

Presumably because instance methods could never be called.

+2
source share

If you put a non-static member in a static class, it will not be a static class. (You cannot create a static class to have non-static members; you would have to be able to instantiate a class on which to call those members that would make it a regular class)

To look at it in another way, marking the class as static, you intentionally ask the compiler not to let you put non-static methods in this class - this is the design decision you made, and the compiler helps to verify that you are following your own design.

+1
source share

I'm not sure if this is related, but (at least in C # .net 4.0) a static class can contain non-static class definitions that contain non-static elements. Thus, it seems that you can add non-static members to a static class, because the nested type is considered member .

Example:

 public static class MyClass { /// <summary> /// This non-static class is accessible and able to be instantiated through the /// static class MyClass. /// </summary> public class Type1 { public String prop1 { get; set; } public String funct1(String result) { return result; } } /// <summary> /// This function is inaccessible since it requires an instance of MyClass. /// It will also cause a compile error. /// </summary> /// <returns></returns> public String nonStaticFunc() { return "aString"; } /// <summary> /// This function is accessible through the MyClass type because it is also static /// and therefore does not require a class instance. /// </summary> /// <returns></returns> public static String staticFunc(String str) { return str; } } 

As you can see, the static class MyClass has a non-static definition of the Type1 class, which must be configured to use. While the static function staticFunc in MyClass does not need an instance to access.

 //Valid operations String result = MyClass.staticFunc("result"); //No class instance. MyClass.Type1 someObj = new MyClass.Type1(); //Class instance from static class containing non-static nested type. someObj.prop1 = someObj.funct1("hi"); 
-one
source share

All Articles