In general, a namespace is a container that provides context for the identifiers (names, or technical terms, or words) it holds, and allows the disambiguation of homonym identifiers residing in different namespaces.[1]
For many programming languages, a namespace is a context for their identifiers. In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory, but one file may have the same name multiple[2] times.
The namespace consists generally of words separated by a period ('.
'). Although the namespace notation resembles the library notation in Java at a first glance, it differs semantically. So, if you write in Java java.servlet.http
, you are using this library; writing java.servlet.*
would include also http
and any other sublibraries of servlet
. This is not the case in namespaces, where a period and asterisk (.*
) at the end is not allowed.
As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace. A namespace is also called a context, because the same name in different namespaces can have different meanings, each one appropriate for its namespace.
Following are other characteristics of namespaces:
Below is an example of a namespace in C++:
namespace Box1{ int boxSide = 4; } namespace Box2{ int boxSide = 12; } int main () { cout << Box1::boxSide << endl; //output 4 cout << Box2::boxSide << endl; //output 12 return 0; }