Namespace Explained

In computing, a names space is a set of signs (names) that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.

Namespaces are commonly structured as hierarchies to allow reuse of names in different contexts. As an analogy, consider a system of naming of people where each person has a given name, as well as a family name shared with their relatives. If the first names of family members are unique only within each family, then each person can be uniquely identified by the combination of first name and family name; there is only one Jane Doe, though there may be many Janes. Within the namespace of the Doe family, just "Jane" suffices to unambiguously designate this person, while within the "global" namespace of all people, the full name must be used.

Prominent examples for namespaces include file systems, which assign names to files.[1] Some programming languages organize their variables and subroutines in namespaces.[2] [3] [4] Computer networks and distributed systems assign names to resources, such as computers, printers, websites, and remote files. Operating systems can partition kernel resources by isolated namespaces to support virtualization containers.

Similarly, hierarchical file systems organize files in directories. Each directory is a separate namespace, so that the directories "letters" and "invoices" may both contain a file "to_jane".

In computer programming, namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality and to avoid name collisions between multiple identifiers that share the same name.

In networking, the Domain Name System organizes websites (and other resources) into hierarchical namespaces.

Name conflicts

Element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.

This XML carries HTML table information:

GoodBad
This XML carries information about a table (i.e. a piece of furniture):

If these XML fragments were added together, there would be a name conflict. Both contain a element, but the elements have different content and meaning.

An XML parser will not know how to handle these differences.

Solution via prefix

Name conflicts in XML can easily be avoided using a name prefix.

The following XML distinguishes between information about the HTML table and furniture by prefixing "h" and "f" at the beginning of the elements.

Good Bad

Mahogany Coffee Table 30 120

Naming system

A name in a namespace consists of a namespace name and a local name.[5] [6] The namespace name is usually applied as a prefix to the local name.

In augmented Backus–Naur form:name = separator When local names are used by themselves, name resolution is used to decide which (if any) particular name is alluded to by some particular local name.

Examples

Examples of names in a namespace
Context Name Namespace name Local name
(domain name) (subdomain name)
Talk Mona Lisa
Path (UNIX) (directory) (file name)
Path (Windows) (directory) (file name)
(C++ namespace) (class)
(C++ namespace) (class)
C# namespace (C# namespace) (class)
(Java package) (class)
(Java package) (class)
(country or territory) (locality)
xmlns:xhtml="http://www.w3.org/1999/xhtml"
(previously declared XML namespace xhtml="http://www.w3.org/1999/xhtml") (element)
(Perl module) (variable)
Uniform Resource Name (URN) (National Bibliography Numbers)
(handle naming authority) (handle local name)
(publisher) (publication)
(organizationally unique identifier) (NIC specific)
(vendor ID) (device ID)
[7] (vendor ID) (product ID)
(previously declared ontology, e.g. by specifying)

Delegation

Delegation of responsibilities between parties is important in real-world applications, such as the structure of the World Wide Web. Namespaces allow delegation of identifier assignment to multiple name issuing organisations whilst retaining global uniqueness.[8] A central Registration authority registers the assigned namespace names allocated. Each namespace name is allocated to an organisation which is subsequently responsible for the assignment of names in their allocated namespace. This organisation may be a name issuing organisation that assign the names themselves, or another Registration authority which further delegates parts of their namespace to different organisations.

Hierarchy

A naming scheme that allows subdelegation of namespaces to third parties is a hierarchical namespace.

A hierarchy is recursive if the syntax for the namespace names is the same for each subdelegation. An example of a recursive hierarchy is the Domain name system.

An example of a non-recursive hierarchy are Uniform Resource Name representing an Internet Assigned Numbers Authority (IANA) number.

Hierarchical namespace breakdown for, an identifier for the book The Logic of Scientific Discovery by Karl Popper, 10th edition.
Registry Registrar Example Identifier Namespace name Namespace
Uniform Resource Name (URN) Formal URN namespace
International Standard Book Numbers as Uniform Resource Names
Bookland
International Standard Book Number (ISBN) German-speaking countries
Agentur für Buchmarktstandards Mohr Siebeck

Namespace versus scope

A namespace name may provide context (scope in computer science) to a name, and the terms are sometimes used interchangeably. However, the context of a name may also be provided by other factors, such as the location where it occurs or the syntax of the name.

Examples of naming systems with local and global scope, and with and without namespaces! !! Without a namespace !! With a namespace
Local scope Filesystem Hierarchy Standard
Global scope Domain Name System

In programming languages

For many programming languages, 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.[9]

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:

As well as its abstract language technical usage as described above, some languages have a specific keyword used for explicit namespace control, amongst other uses. Below is an example of a namespace in C++:

import std;

// This is how one brings a name into the current scope. In this case, it's// bringing them into global scope.using std::println;

namespace box1

namespace box2

int main

Computer-science considerations

A namespace in computer science (sometimes also called a name scope) is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e. names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its definition) belongs.[10]

This concept can be illustrated with an analogy. Imagine that two companies, X and Y, each assign ID numbers to their employees. X should not have two employees with the same ID number, and likewise for Y; but it is not a problem for the same ID number to be used at both companies. For example, if Bill works for company X and Jane works for company Y, then it is not a problem for each of them to be employee #123. In this analogy, the ID number is the identifier, and the company serves as the namespace. It does not cause problems for the same identifier to identify a different person in each namespace.

In large computer programs or documents it is common to have hundreds or thousands of identifiers. Namespaces (or a similar technique, see Emulating namespaces) provide a mechanism for hiding local identifiers. They provide a means of grouping logically related identifiers into corresponding namespaces, thereby making the system more modular.

Data storage devices and many modern programming languages support namespaces. Storage devices use directories (or folders) as namespaces. This allows two files with the same name to be stored on the device so long as they are stored in different directories. In some programming languages (e.g. C++, Python), the identifiers naming namespaces are themselves associated with an enclosing namespace. Thus, in these languages namespaces can nest, forming a namespace tree. At the root of this tree is the unnamed global namespace.

Use in common languages

C

It is possible to use anonymous structs as namespaces in C since C99.

  1. pragma once

const struct Math;

  1. include

static double _sin(double arg)

const struct Math = ;

  1. include
  2. include "Math.h"

int main

C++

In C++, a namespace is defined with a namespace block.[11] namespace abc 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 namespace abc, bar must be written abc::bar to be accessed. C++ includes another construct that makes this verbosity unnecessary. By adding the lineusing namespace abc;to a piece of code, the prefix abc:: is no longer needed.

Identifiers that are not explicitly declared within a namespace are considered to be in the global namespace.int foo;These identifiers can be used exactly as they are declared, or, since the global namespace is unnamed, the namespace specifier :: can be prefixed. For example, foo can also be written ::foo.

Namespace resolution in C++ is hierarchical. This means that within the hypothetical namespace food::soup, the identifier Chicken refers to food::soup::Chicken. If food::soup::Chicken doesn't exist, it then refers to food::Chicken. If neither food::soup::Chicken nor food::Chicken exist, Chicken refers to ::Chicken, an identifier in the global 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 because it did not exist in early versions of the language. For example, the entire C++ Standard Library is defined within namespace std, but before standardization many components were originally in the global namespace. The using statement can be used to import a symbol into the current scope.

The use of the using statements in headers for reasons other than backwards compatibility (e.g., convenience) is considered to be against good code practices, as those using statements propagate into all translation units that include the header. However, modules do not export using statements unless explicitly marked export, making using statements safer to use. For instance, one can import a module wikipedia.project.util with matching namespace wikipedia::project::util and then use using statements on symbols from that namespace to simplify verbose namespaces. Note that unlike other languages like Java or Rust, C++ modules, namespaces and source file structure do not necessarily match, though it is convention to match them for clarity (for example, module abc.def.uvw.XYZ matches namespaced class abc::def::uvw::XYZ and resides in file abc/def/uvw/XYZ.cppm).

using should be used to simplify verbose nested namespaces when modular translation units are used.

export module wikipedia.project.App;

import std;

import wikipedia.project.fs;import wikipedia.project.util;

using wikipedia::project::fs::File;using wikipedia::project::util::ConfigLoader;using wikipedia::project::util::logging::Logger;using wikipedia::project::util::logging::LoggerFactory;

export namespace wikipedia::project

C++11 introduces inline namespaces, which is such that its members are treated as if they are also members of the enclosing namespace. It is declared by writing inline namespace. It is akin to an implicit using namespace statement, meaning qualifying symbols in it is optional.

The inline property is transitive. If a namespace a contains an inline namespace b, which in turn contains another inline namespace c, then members of c can be accessed as if they were members of a or b.

A primary use case for inline namespaces is ABI compatibility and versioning. By placing different versions of an API within distinct inline namespaces (e.g., v1, v2), and then making the currently desired version inline, library developers can manage ABI compatibility. When a new version is released, the inline keyword can be moved to the new version's namespace, allowing users to automatically link against the new version while still enabling access to older versions through explicit qualification.

namespace mylib::utils

int main

C#

Namespaces are heavily used in C# language. All .NET Framework classes are organized in namespaces, to be used more clearly and to avoid chaos. Furthermore, custom namespaces are extensively used by programmers, both to organize their work and to avoid naming collisions.When referencing a class, one should specify either its fully qualified name, which means namespace followed by the class name:System.Console.WriteLine("Hello World!");int i = System.Convert.ToInt32("123");

or add a using statement. This eliminates the need to mention the complete name of all classes in that namespace.

using System;

Console.WriteLine("Hello World!");int i = Convert.ToInt32("123");

In the above examples, System is a namespace, and Console and Convert are classes defined within System.

Unlike C++, using can only import all symbols in a namespace (much like using namespace from C++, use * in Rust, or import * in Java). It cannot be used to import individual symbols and classes like it is used in Java.

namespace Wikipedia.Project;

using System;using System.IO;

using Microsoft.Extensions.Logging;

using Wikipedia.Project.Utility;

class App

Unlike C++, C# namespaces do not allow relative referencing of symbols. For example, the class Foo.Bar.Baz.Qux cannot be referred to as Baz.Qux even if referred to from within namespace Foo.Bar: either the namespace Foo.Bar.Baz must be imported to refer to class Qux, or Foo.Bar.Baz.Qux must be fully qualified.

Java

In Java, the idea of a namespace is embodied in Java 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 class String in package [[java.lang]] can be referred to as java.lang.String (this is known as the fully qualified class name). Like C++, Java offers a construct that makes it unnecessary to type the package name (import). However, certain features (such as reflection) require the programmer to use the fully qualified name.

Unlike C++, namespaces in Java are not hierarchical as far as the syntax of the language is concerned. However, packages are named in a hierarchical manner. For example, all packages beginning with java are a part of the Java platform—the package contains classes core to the language, and contains core classes specifically relating to reflection.

In Java (and Ada, C#, and others), namespaces/packages express semantic categories of code. For example, in C#, 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.

Function and class scopes can be viewed as implicit namespaces that are inextricably linked with visibility, accessibility, and object lifetime.

In Java, packages cannot be partially qualified like they can in C++. For instance, it is not possible to import the java namespace and then refer to java.util.ArrayList as util.ArrayList. Symbols must either be fully qualified or imported completely into scope. import statements are not transitive nor can they be deliberately marked export like in C++. All import statements must appear at the beginning of the file, and cannot be written at any other scope. This is in contrast to C++, where the std namespace can be imported by writing using namespace std;, and then std::chrono::system_clock can be referred to as chrono::system_clock.

package org.wikipedia.project;

import java.nio.file.Paths;import java.util.logging.Level;import java.util.logging.Logger;

import org.wikipdia.project.util.ConfigLoader;

public class App

Because Java does not support independent functions outside of classes, static class methods and so-called "utility classes" (classes with private constructors and all methods and fields static) are the equivalent to C++-style namespaces. Some examples are java.lang.Math, which contains the constants like Math.PI and methods like Math.sin.

import statements can be used to import all symbols in a package, called a glob import, which is similar to using namespace in C++. For instance, writing import java.util.*; imports all classes in the java.util package. This however, can cause symbol pollution within a file. Furthermore, using glob import statements on packages that have classes with the same name can cause ambiguity, and will fail to compile. However, java.lang.* is implicitly imported into all Java source files by default.

import java.sql.*; // Imports all classes in java.sql, including java.sql.Dateimport java.util.*; // Imports all classes in java.util, including java.util.Date

Date d = new Date; // Ambiguous Date reference resulting in compilation error

// Instead, the fully-qualified names must be used:java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis);java.util.Date utilDate = new java.util.Date;

PHP

Namespaces were introduced into PHP from version 5.3 onwards. Naming collision of classes, functions and variables can be avoided.In PHP, a namespace is defined with a namespace block.

  1. File phpstar/foobar.php

namespace phpstar;

class FooBarWe can reference a PHP namespace with the following different ways:

  1. File index.php
  2. Include the file

include "phpstar/foobar.php";

  1. Option 1: directly prefix the class name with the namespace

$obj_foobar = new \phpstar\FooBar;

  1. Option 2: import the namespace

use phpstar\FooBar;$obj_foobar = new FooBar;

  1. Option 2a: import & alias the namespace

use phpstar\FooBar as FB;$obj_foobar = new FB;

  1. Access the properties and methods with regular way

$obj_foobar->foo;$obj_foobar->bar;

Python

In Python, namespaces are defined by the individual modules, and since modules can be contained in hierarchical packages, then namespaces are hierarchical too.[12] [13] In general when a module is imported then the names defined in the module are defined via that module's namespace, and are accessed in from the calling modules by using the fully qualified name.

  1. assume ModuleA defines two functions : func1 and func2 and one class : Class1

import ModuleA

ModuleA.func1ModuleA.func2a: ModuleA.Class1 = Modulea.Class1

The from ... import ... statement can be used to insert the relevant names directly into the calling module's namespace, and those names can be accessed from the calling module without the qualified name:

  1. assume ModuleA defines two functions : func1 and func2 and one class : Class1

from ModuleA import func1

func1func2 # this will fail as an undefined name, as will the full name ModuleA.func2a: Class1 = Class1 # this will fail as an undefined name, as will the full name ModuleA.Class1Since this directly imports names (without qualification) it can overwrite existing names with no warnings.

A special form of the statement is from ... import * which imports all names defined in the named package directly in the calling module's namespace. Use of this form of import, although supported within the language, is generally discouraged as it pollutes the namespace of the calling module and will cause already defined names to be overwritten in the case of name clashes,[14] though using from import in Python can simplify verbose namespaces, such as nested namespaces.

from selenium.webdriver import Firefoxfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.remote.webelement import WebElement

if __name__

"__main__": driver: Firefox = Firefox element: WebElement = driver.find_element(By.ID, "myInputField") element.send_keys(f"Hello World") action: ActionChains = ActionChains(driver) action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform

Python also supports import x as y as a way of providing an alias or alternative name for use by the calling module:

import numpy as npfrom numpy.typing import NDArray, float32

a: NDArray[float32] = np.arange(1000)

Rust

In Rust, a namespace is called a "module" and declared using mod. Symbols inside the module are by default private, and cannot be accessed externally, unless declared with the pub keyword which exposes them. Modules can have sub-modules inside of them, allowing for nested namespaces.

Similar to the using keyword in C++, Rust has the use keyword to import symbols into the current scope.mod my_module

fn main

Writing mod util; indicates to the compiler to find either a file named util.rs or util/mod.rs. crate in a use statement refers to the root of the current "crate" (project), while super can be used to refer to the parent module.

mod util;

use std::fs::File;

use crate::util::ConfigLoader;use crate::util::logging::;

pub struct App

impl App

The use keyword in Rust is more versatile than its counterpart using in C++. In addition to importing single symbols, symbol aliasing with as and glob imports, use can import multiple symbols on the same line using braces (which may be nested), import individual namespaces, and do all of the above in a single statement. This is an example of using all of the above:

use std::;

XML namespace

See main article: XML namespace.

In XML, the XML namespace specification enables the names of elements and attributes in an XML document to be unique, similar to the role of namespaces in programming languages. Using XML namespaces, XML documents may contain element or attribute names from more than one XML vocabulary.

SAP Namespace

In SAP systems (especially ABAP environments), namespaces are used to prevent naming collisions between standard SAP-delivered objects and customer or partner developments.[15]

A namespace identifier is delimited with “/” (for example `/MYNS/`) and is reserved via SAP’s namespace registration process. Once reserved, objects created under that namespace are uniquely identifiable and protected from unintended overwrite by SAP upgrades or imports.[16]

In modern SAP landscapes (such as ABAP in the cloud and HDI containers), namespaces are also used to semantically group development artifacts or bundles.

Emulating namespaces

In programming languages lacking language support for namespaces, namespaces can be emulated to some extent by using an identifier naming convention. For example, C libraries such as libpng often use a fixed prefix for all functions and variables that are part of their exposed interface. Libpng exposes identifiers such as:

png_create_write_struct png_get_signature png_read_row png_set_invalid

This naming convention provides reasonable assurance that the identifiers are unique and can therefore be used in larger programs without naming collisions.[17] Likewise, many packages originally written in Fortran (e.g., BLAS, LAPACK) reserve the first few letters of a function's name to indicate the group to which the function belongs.

This technique has several drawbacks:

It also has a few advantages:

See also

Notes and References

  1. Siddharth . Aniket . Anushka . Aditya . Chaya . Deepa . Batman . Cermak . Ronnie . Chaiken . John . Douceur . Jon . Howell . Jacob . Lorch . Marvin . Theimer . Roger . Wattenhofer . FARSITE: Federated, Available, and Reliable Storage for an Incompletely Trusted Environment . Proc. USENIX Symp. on Operating Systems Design and Implementation . 2002 . The primary construct established by a file system is a hierarchical directory namespace, which is the logical repository for files. . https://web.archive.org/web/20100728202945/http://paul.rutgers.edu/cs545/S06/papers/adya-farsite-osdi-2002.pdf . 2010-07-28 .
  2. Web site: C# FAQ: What is a namespace . C# Online Net . 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. . 2010-02-23 . https://web.archive.org/web/20131020150529/http://tutorials.csharp-online.net/index.php?title=CSharp_FAQ%3A_What_is_a_namespace . 2013-10-20 .
  3. Web site: An overview of namespaces in PHP. PHP Manual. What are namespaces? In the broadest definition, namespaces are a way of encapsulating items. This can be seen as an abstract concept in many places. For example, in any operating system directories serve to group related files, and act as a namespace for the files within them..
  4. Web site: Creating and Using Packages. Java Documentation. Oracle. A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this lesson simply as classes and interfaces..
  5. Web site: Namespaces in XML 1.0 (Third Edition) . XML Core Working Group . W3C . 8 December 2009 . 2012-03-30.
  6. URN Syntax . 2141 . Syntax . 2 . 1 . Moats . Ryan . May 1997 . . 2012-03-30.
  7. Stephen J. Gowdy."List of USB ID's".2013.
  8. Functional Requirements for Uniform Resource Names . 1731 . Requirements for functional capabilities . 2 . 3 . Sollins & Masinter . December 1994 . . 2012-03-30.
  9. Web site: C# FAQ: What is a namespace. C# Online Net. https://web.archive.org/web/20131020153832/http://tutorials.csharp-online.net/CSharp_FAQ:_What_is_a_namespace. October 20, 2013. 2010-02-23. For instance, [under [[Microsoft Windows|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 SystemWeb namespace..
  10. Web site: A namespace is "a logical grouping of the names used within a program.". 10 April 2002 . Webopedia.com. 2011-07-26.
  11. Web site: Namespaces allow to group entities like classes, objects and functions under a name. . Cplusplus.com . 2011-07-26.
  12. Web site: 6. Modules. The Python Tutorial. Python Software Foundation. 25 October 2010.
  13. Web site: Python Scopes and Namespaces . Docs.python.org . 2011-07-26.
  14. https://docs.python.org/3/tutorial/modules.html "in general the practice of importing * from a module or package is frowned upon"
  15. Web site: SAP Namespace Overview . SAPVista . SAP systems use namespaces to prevent naming conflicts between SAP standard objects and customer-specific developments. . 7 October 2025.
  16. Web site: SAP Help Portal – ABAP Connectivity and Namespace Documentation . SAP Help Portal . In the SAP Application Interface Framework, namespaces are used for the logical structuring of objects and configurations. . 7 October 2025.
  17. Web site: Why I Hate Namespaces . Danny Kalev . bot: unknown . https://web.archive.org/web/20160709115609/http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=286 . 2016-07-09 .