Static vs. Const, a C++ Point of View
During my time programming I have many times seen a lot of confusion regarding the concept of static and const declarations. This is mainly from the C++ language, however, many other languages have similar concepts as well, mostly the high level ones. With this article I hope to clear up some of the differences between the use of const and static within the C++ language, some might learn, others might recognize and hopefully yet others can relate it to other programming languages as well.
So what does static and const do then that makes them different?
I will start with static as I've been mentioning it a lot in my last paragraph.
The static we are talking about is the declaration itself of course, "static int a" for instance. The static meaning people are confusing it with is when a type of variable is considered static within object oriented programming and it can't hold different types of variables while a dynamic type can hold many different types of variables as long as they are subclasses to the variable type. The meaning there refers to the fact that the type of value the variable will hold will remain unchanged even though the value itself of the variable can change. The static that is used in declarations refers to another kind of implementation of the concept of not changing and it is that what has been declared as static will basically only have one instance created of itself that everything uses. For classes this basically means that you can have a variable in your class, declare it static and that means that it will instead of being recreated for every single object that will exist, only one instance of it will be made which every object will then be using. This means that if you change it, it will change for all other objects as well, this is obviously good for when you have a variable that should be the same for every object as it's more space efficient and if it needs changing you only have to change it once and not for all. However, the static declaration doesn't necessarily have to be used in conjunction with classes and objects. You can in fact use it anywhere, for example you could use it in a function you have made. If used within a function, the static variable will only be declared once and if you've assigned a value to it in your declaration then it'll only be assigned at the beginning. It means that the function does not create a new instance of that variable each time, but instead makes one and then when called on later it will still have the value which was there last time it was called. This is good when a variable needs to remain the way it last was, but you want to or have to store it inside a separate function.
To give it a shorter overall explanation; the static declaration will let you use something basically as if it was globally declared, but just in the scope of its function or file, not necessarily the entire program. This allows the rest of the program to deal with global variables and similar the way it's intended and for you to override it in a function while leaving its functionality as global intact for that one case. What is considered unchanging (ergo static) here is, for those who haven't deduced it yet, that it only creates one version of something, one place that doesn't change for anything no matter what instance of it is trying to use it, though its contents can still be manipulated.
Now that we have covered static we will move on to const.
Const, as I've previously mentioned, stand for constant. This is the straightforward meaning that most people think of when they think something should remain unchanged. When something is declared as const it means that we can not change its contents, it's simple as that. This has mainly been implemented just to help programmers to not mistakenly alter something that should not be changed, which is especially handy on large projects where multiple people work on the same thing. When using const, you can of course use it as you wish in C++ on not only variables to make their value constant, but you can also use it on pointers. A rule to be noted is that when you use const in your declarations, whatever is on the left side of it will be the thing declared as const, except if const is the first thing written in the declaration, in which case whatever is on the right side. This means that "const int a" is equivalent to "int const a". For pointers it'd mean that "int const *a" is a pointer to a constant value since the int is declared as const, so you won't be able to change the value pointed to; meanwhile "int* const a" declares a constant pointer to a value, meaning that the pointer itself held can't be changed, but the value it points to can. Of course const can be used several times to declare both as constant.
This concept of unchanging is what most people are after and as you can notice it is vastly different from the static declaration.
Some other languages obviously have keywords similar to const, but seem to differ in both the declaration's name and sometimes in minor functionalities, while static usually remains the same in the languages that use it even down to its name. For those using java, the declaration that tries to mimic const is called final and here too the confusion remains between final and static. I've even seen tutorial sites claiming them to be the same. Even though final isn't exactly the same as const, the main differences still remain, but I'll mention what mainly makes final differ from const. In java when you declare something final it is only usable for variables holding objects, however, these never hold objects themselves, just the pointer to the object. What this means basically is that once declared you can't change what object it points to, but the object itself can be changed. Since java can't really use basic variable types this leads to the fact that you can't declare a variable or the object in itself as constant. It's noteworthy however that java does have the const keyword reserved, but has not given it any actual implementation.
I hope this has cleared up the basic difference between const and static and all know roughly what they do. Const makes something unchangeable, while static makes one single thing which all instances of where it's declared will be sharing. Differences like these are why one shouldn't always assume things purely because of grammatical logic when it comes to programming. As for static vs. dynamic languages, that's a story for another article.