JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

What Is an Unnamed Package?

This section describes what is an unnamed package - a package of compilation units with no package name. The main question is how to refer to a compilation unit in an unnamed package.

Unnamed packages contain compilation units (classes or interfaces) that have no package declaration statement. For example, the following class is a compilation unit in an unnamed package, Hello.java:

class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
   }
}

There are a number of questions about unnamed packages we need to answer:

  • How many unnamed packages can JDK support?
  • How compilation units in each unnamed package are stored?
  • How to refer to the compilation units in unnamed packages?

Based on the documentation of JDK for Windows system, my guess is that JDK supports multiple unnamed packages. Each unnamed package is stored in the top directory of each class path specified in one of the following three ways:

  • -classpath path;path;...
  • set CLASSPATH=path;path;...
  • "current directory"

Let's do some tests to answer these questions and to confirm my guess in next sections.

Sections in This Chapter

What Is an Unnamed Package?

One Class in an Unnamed Package - Hello.java

Two Classes in Unnamed Packages - Hello.java and CallingHello.java

Importing Classes Defined in Unnamed Packages

Importing Classes from Unnamed to Named Packages

Importing Classes from Unnamed to Named Packages - JDK 1.4.1

Dr. Herong Yang, updated in 2008
What Is an Unnamed Package?