Name collisions in a flat global scope
A namespace is a logical container that groups classes and interfaces to give them a unique context and prevent name collisions. Without namespaces, two Usuario classes or two Date classes compete for the same short name in a single flat global scope.
Collision: the compiler cannot tell which Usuario (or Date) class you mean.
The folder must match the package line
Unlike other languages, Java enforces a strict rule: the namespace declared in package must match the directory path on the classpath exactly. Pick a file from the tree to see its required declaration.
- com
- facundouferer
- tienda
- dominio
- servicio
- tienda
- facundouferer
package com.facundouferer.tienda.dominio; FQCN: com.facundouferer.tienda.dominio.Producto The FQCN is built piece by piece
To the JVM, Producto is only a local label. Its formal identity to the ClassLoader is the Fully Qualified Class Name (FQCN): reversed domain, project, layer and class, joined by dots.
Tap "Add next piece" to start building the FQCN.
com.facundouferer.tienda.dominio.Producto your own domain entity com.proveedor.catalogo.Producto DTO imported from an external catalog import does not load code: it registers an alias
The import statement is a lexical shortcut so you never have to type the FQCN each time. Java forbids importing two classes with the same short name in one file: try the three scenarios.
package com.facundouferer.tienda.servicio;
import java.util.Date;
public class AuditoriaServicio {
private Date fechaOperacion = new Date();
} Dateβjava.util.Datepackage com.facundouferer.tienda.servicio;
import java.util.Date;
import java.sql.Date; // β Date is already defined java.sql.Dateβcollision with the previous importpackage com.facundouferer.tienda.servicio;
import java.util.Date; // Date sin calificar = java.util.Date
public class AuditoriaServicio {
private Date fechaOperacion = new Date();
private java.sql.Date fechaPersistenciaBD = new java.sql.Date(System.currentTimeMillis());
} Dateβjava.util.Datejava.sql.Dateβjava.sql.Date (inline FQCN, no import)public vs package-private: trust boundaries
Namespaces are not just cosmetic folders: they are also visibility boundaries. The default modifier (package-private) restricts access strictly to classes in the same package. Pick a class and an access point to test it.
Core rules for package and namespace design
Five practical rules to keep your namespaces unique, readable and easy to navigate. Tap each rule to see a good and a bad example.
tienda.dominio.Producto com.facundouferer.tienda.dominio.Producto Tienda.Dominio tienda.dominio Utilidades.java contains public class Producto Producto.java contains public class Producto tienda.interfaces / tienda.clases tienda.dominio / tienda.servicio import static java.lang.Math.*; import static java.lang.Math.PI;