It's worth noting that const behaves completely differently in C
. In C, it behaves more like a "special" global variable
. That is to say, you can declare a constant in one file and use "extern" to access it in another file. You also can't do this:
const int ARRAYSIZE = 10;
In the C++ world, everyone hates the preprocessor
, so Stroustrop
"hijacked" the const keyword to replace the use of #define for constants. All constants are module-local (like they had the "static" keyword used). To get the same behavior as is default in C, you have to do something like this:
extern int EXTERNALLY_ACCESSIBLE_CONSTANT = 1337;
This means that you can put constants into header files more easily.
The designers of Java quite understandably wanted nothing to do with all this mind-breaking inconsistency, which is why constants in Java use the keyword "final" instead.