Why Using the Static Keyword isn’t Actually Real Sharing

The legendary static keyword has it’s origins as far back as C programming. This famous keyword has been carried across to C++, Java, and among many others — although the meaning has changed a bit along the way.

One of the most common uses is for class level sharing of members. Static members don’t really belong to any instances, and can only really be accessed directly from  the class. Also or from another definition in the same class.

This is good and all for sharing within a class, but the issue is that it doesn’t play too well with inheritance. This is because static members can’t be overridden in Java. Also, inheritance is another common method for achieving code sharing.

You can consider the following code snippet. This will show how one might hope to override the static members in C#.

public class Base
{
    public static virtual string Value => "Base";
    public static virtual void Display() =>
        Console.WriteLine(Value);
}
public class Sub1 : Base
{
    public static override string Value => "Sub1";
}
public class Sub2 : Base
{
    public static override void Display() =>
        Console.WriteLine($">>> {Value}");
}
new Sub1().Display(); // Sub1
new Sub2().Display(); // >>> Base

I know that I would love to be able to override one or both of the value and/or the display members. Unfortunately, this wont allow us to compile because the static members can’t be virtual. The programming language Python actually got this right. This is thanks to the classmethod decorator.

class Base:
    value = 'base'
    @classmethod
    def display(cls):
        print(cls.vlaue)
class Sub1(Base):
    value = 'Sub1'
class Sub2(Base):
    @classmethod
    def display(cls):
        print('>>>', cls.value)
Sub1().display() # Sub1
Sub2().display() # >>> Base

Now allow me to ask, how come Python can do this but not C#? Notice in the above code snippet, we used the classmethod decorator. Also take notice that we didn’t use the staticmethod decorator. The classmethod benefits directly from the fact that it passed a reference to the class. The staticmethod would make us hard code it into the source code.

This overall distinction really does highlight the real meaning of the word static when it comes to programming. The static of static analysis, static type checking, static libraries, and static memory allocation. Static as in not dynamic, known before the run time, often at compile time.

The overall reason why static members can’t be overridden is because doing so requires knowing at run-time which override that you are wanting to use. This goes against the definition of the word static. As an example, even in the language like Python without compilation, the concept of static still exists and will have the same restrictions.

So if I want to get truly shareable members, both horizontally across members of a class and vertically across a type hierarchy, we are going to need another solution to do so.

Advertisements


Categories: Programming

Tags: , , , , , ,

%d bloggers like this: