|
|
In early programming languages, such as C, namespaces were defined implicitly by the semantics of a program. For example, an identifier defined within a function only has meaning within the context of that function. These implicitly defined namespaces were inextricably linked with the ideas of visibility, accessibility, and lifetime (see scope).
As programming progressed and software projects became more complex, a need was seen for a language construct which separated visibility from context. Before namespaces, naming collisions were avoided by prefixing characters to an identifier in various nonstandard ways. This practice encumbered readability. Therefore, namespaces have been added to most modern languages.
In C++, a namespace is defined with a namespace block.
Within this block, identifiers can be used exactly as they are declared. Outside of this block, the namespace specifier must be prefixed. For example, outside of
to a piece of code, the prefix
Code that is not explicitly declared within a namespace is considered to be in the default namespace.
Namespaces in C++ are hierarchical. Within the hypothetical namespace
Namespaces in C++ are most often used to avoid naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this facility. For example, the entire standard library is defined within
In Java, the idea of a namespace is embodied in packages. All code belongs to a package, although that package need not be explicitly named. Code from other packages is accessed by prefixing the package name before the appropriate identifier, for example
Unlike C++, namespaces in Java are not heirarchical as far as the syntax of the language is concerned. However, packages are named in a hierarchical manner. For example, all packages begining with
In Java (as well as Ada, C#, and others), namespaces/packages express semantic categories of code. For example, in C#,
Although it is not a programming language, XML makes extensive use of namespaces.
History
Use in common languages
namespace foo {
int bar;
}
namespace foo, bar must be written foo::bar. C++ includes another construct which makes this verbosity unnecessary. By adding the lineusing namespace foo;foo:: is no longer needed.food::fruit, the identifier orange refers to food::fruit::orange if it exists, or if not food::orange if it exists. If neither exist, orange refers to an identifier in the default namespace.namespace std, and in earlier standards of the language, in the default namespace.class String in package java.lang can be refered to as java.lang.String (this is known as the fully qualified class name). Like C++, Java offers a construct which makes it unneccesary to type the package name (import). However, certain features (such as reflection) require the programmer to use the fully qualified name.java are a part of the Java platform, the package java.lang contains classes core to the language, and java.lang.reflect contains core classes specifically relating to reflection.namespace System contains code provided by the system (the .NET framework). How specific these categories are and how deep the hierarchies go differ from language to language.