Why can static classes can have only static members?
Asked Answered
D

6

8

As the title suggests I would like to understand why static classes can have only static members. I hope it is same in all the programming languages. So the explanation would be the same for all the languages, I believe.

Draff answered 11/12, 2010 at 6:44 Comment(2)
Sounds a bit homeworky... have you thought this through? What purpose would there be to allow non-static members in a static class?Salsbury
I know the rule 'static class cannot have non-static members'. But i wanted to understand why. In fact i expected more geeky answers in terms of memory as how static classes and their members are stored and how non-static classes and members are stored. :)Draff
T
16

Static classes cannot be instantiated, meaning they do not have any instances. Non-static members require an instance of their class to be accessed. Since static classes cannot have instances, you cannot access any non-static members if they exist.

Therefore, static classes can only contain static members.

Trowbridge answered 11/12, 2010 at 6:48 Comment(0)
W
7

It's not a design decision, so much as a logical one. The easiest place to start is by looking at the relevant definitions of the concepts:

A static class is one that cannot be instantiated. That means you cannot create objects that are of that class's type.

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

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

However, you can have non-static classes that contain static members. This way, you can access the data or call the methods exposed as static members without instantiating an object of that class. However, you could also instantiate an object of that class's type and access non-static (or instance) members. For example, if you had a class Circle, you could have static members like a CalculateArea function and a PI field. These members are applicable to all circles, generally, just by virtue of the fact that they are circles. But you could also have non-static members that are associated with specific instances of that class, because they describe specific circle objects. These could be the fields Diameter and Circumference. You could also have non-static functions that calculate the area of the circle, given the data stored in the non-static fields for that particular instance.

Westernmost answered 11/12, 2010 at 6:51 Comment(0)
P
4

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

If you want to mix and match static members, don't make the class static.

Paulson answered 11/12, 2010 at 6:48 Comment(0)
B
2

Presumably because instance methods could never be called.

Beesley answered 11/12, 2010 at 6:48 Comment(0)
O
1

If you put a non-static member in a static class, it wouldn't be a static class. (You cannot instantiate a static class - to have non-static members, you would have to be able to make instances of the class on which to call those members - which would make it a regular class)

To look at it another way, by marking a class as static, you are deliberately asking for the compiler not to allow you to put non-static methods in that class - this is a design decision you have made, and the compiler helps check that you follow your own design.

Oloroso answered 11/12, 2010 at 6:48 Comment(0)
H
-1

I am not sure if this is related but (in c# .net 4.0 at least) a static class can contain non-static class definitions which contain non-static members. So it seems that you can add non-static members to a static class because a nested type is considered a 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 class definition Type1 that must be instanced in order to use. Whereas the static function staticFunc in MyClass does not need an instance to be accessed.

//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");
Helmholtz answered 2/5, 2012 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.