Namespace

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;
}

See also

References

  1. ^ "C# FAQ: What is a namespace". http://en.csharp-online.net/: C# Online Net. http://en.csharp-online.net/CSharp_FAQ:_What_is_a_namespace. Retrieved 2010-02-23. "A namespace is nothing but a group of assemblies, classes, or types. A namespace acts as a container—like a disk folder—for classes organized into groups usually based on functionality. C# namespace syntax allows namespaces to be nested." 
  2. ^ "C# FAQ: What is a namespace". http://en.csharp-online.net/: C# Online Net. http://en.csharp-online.net/CSharp_FAQ:_What_is_a_namespace. Retrieved 2010-02-23. "For instance, [under Windows], to access the built-in input-output (I/O) classes and members, use the System.IO namespace. Or, to access Web-related classes and members, use the System.Web namespace."