Sub topic 13
Why to Avoid the Default Package
Assume you can write and run a basic Java class and understand classes, methods, and fields. This chapter focuses on the default package, also called the unnamed package, and why real projects should use named packages instead.
What the Default Package Is
A Java source file has no package declaration when it starts directly with an import or a class declaration:
public class QuickTest {
public static void main(String[] args) {
System.out.println("Running");
}
}QuickTest belongs to the default package. Its fully qualified name is simply QuickTest, because it has no package prefix.
This is convenient for a small experiment. You can compile and run it from the same directory:
javac QuickTest.java
java QuickTestThe convenience becomes a problem as soon as the project grows.
Named Packages Cannot Use Default-Package Types
A class in a named package cannot import a class from the default package. For example, this declaration does not provide a valid way for com.example.app.Main to use QuickTest:
package com.example.app;
// A class in a named package cannot import QuickTest.The restriction prevents a named package from depending on a type whose namespace is not identified. Code in the default package can use classes from named packages, but that one-way relationship does not make the default package a useful place for shared code.
This creates a migration trap. If you begin with several default-package classes and later move the main application into a named package, those old classes must also be moved or redesigned before the application can use them.
Build Tools and Project Structure
Build tools expect source code to have a predictable namespace and directory structure. A named package gives each class a stable identity:
| Source declaration | Fully qualified name | Typical source path |
|---|---|---|
| `package com.example.app;` | `com.example.app.Main` | `src/com/example/app/Main.java` |
The default package has no package directory. That makes it harder to separate application code, tests, libraries, and generated code. It also increases the chance that two classes with the same simple name will collide.
Named packages work consistently with IDEs, JAR files, dependency analysis, and Java modules. They make ownership clearer. For example, com.example.shop.model.Product tells you that Product belongs to the shop application's model area.
Move Code into a Named Package
A small project can be converted by adding a package declaration and moving the file into the matching directory:
src/
└── com/
└── example/
└── app/
└── Main.javaMain.java:
package com.example.app;
public class Main {
public static void main(String[] args) {
System.out.println("Running in a named package");
}
}Compile and run it from the project directory:
javac -d out src/com/example/app/Main.java
java -cp out com.example.app.MainThe -d out option tells javac where to write class files. The compiler creates the package directories under out. The -cp out option tells java where to find those files, and the command uses the class's fully qualified name.
Package Naming Rules
Use the reverse domain name convention when your organization owns a domain. For example:
com.example.shoporg.example.toolsio.example.weather
The convention reduces collisions between independently developed libraries. Use lowercase package names, and separate parts with periods. Do not use spaces, hyphens, or class-style capitalization.
Avoid putting reusable classes in the default package. It is suitable for a short command-line experiment, but named packages are the safer starting point for application code.
Exercise
Why can a class in com.example.app not directly import a class from the default package?
Show answer
Java does not allow named packages to import types from the unnamed package. Move the class into a named package whose declaration matches its directory.