The keyword 'static' can be confusing because in C, where it originated, it has several meanings. When used to declare a variable in a function, it means that the variable has a lifetime outside the function. It is essentially global, which is private to the function. If the global variable is static, it is essentially private to this source file. In both cases, the variable has one memory location, as global. It is just that the compiler does not allow you to access it either outside the function or compilation.
I guess the word βstaticβ was used because the location of the static variable never changes, unlike a regular local variable that will take place in memory somewhere on the stack depending on how the stack looked when the function was called.
When C ++ was created, this keyword was redistributed to refer to class properties and methods. I suppose the thought was that the static method or property was kind of global, which was closed to the class. If you think about how they are laid out in memory, this makes some sense, since a static property will have one address, like a global variable. The only difference is that the compiler does not allow you to use it outside the class.
Since Java (and other languages) had syntax inspired by C ++, it also uses "static" to denote class methods and properties. This is sad because the use of this keyword has little to do with the English meaning.
But overall, this is a way to look at it. In most languages, if it is "static", in one program there is only one of them. Think of it as something with one fixed memory address.
Steven burnap
source share