Tuesday, April 19, 2011

Java vs C++ static

Static fields and methods have the same functionality in Java and C++. However, the syntax is slightly different. 
In C++, you use the :: operator to access a static field or method outside its scope, such as Math::PI.

The term "static" has a curious history. 
  • At first, the keyword static was introduced in C to denote local variables that don't go away when a block is exited. In that context, the term "static" makes sense: the variable stays around and is still there when the block is entered again. 
  • Then static got a second meaning in C, to denote global variables and functions that cannot be accessed from other files. The keyword static was simply reused, to avoid introducing a new keyword. 
  • Finally, C++ reused the keyword for a third, unrelated, interpretation—to denote variables and functions that belong to a class but not to any particular object of the class. That is the same meaning that the keyword has in Java.

No comments:

Post a Comment