Static Keyword in C, C++, C#, and Java – Differences Explained

historylanguage-designprogramming-languages

To me, the use of the keyword static in C and languages like C# and Java are "false friends" like "to become" in English and "bekommen" in German (= "to get" in English), because they mean different things.

In C static means, that the function or variable is only accessible via functions inside the same source file, comparable to private functions and members in C++, Java and C#.

In C++, Java and C# static means, that the methods are not members of a class instance, but effectively are more or less like C functions plus namespace.

IMHO these two concepts are quite different, so why did the designers of C++ and later Java and C# choose the static keyword for that behaviour? Is there a logical connection that I miss?

EDIT
I know, that static in C does not govern accessability in a way similar to private in C++, but can be used in that way, see https://stackoverflow.com/a/1479639/124983

Best Answer

I have a book about the design of C++ by Bjarne Stroustrup (the inventor of C++). I don't have it right here so I can't lookup the exact quote right now, but in it he admits that when he added static to C++, he didn't fully understand what it meant in C. So that's why in C++ it has a different meaning than in C.

Java and C# inherited the meaning from C++.