JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Unnamed Packages

Part:   1  2  3   4 

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

(Continued from previous part...)

Importing Classes Defined in Unnamed Packages

For classes defined in unnamed packages, their simple names are identical to their fully qualified names. Therefore, there is no need to use import statement in the compilation units where they are referred.

But if you really want to use an import statement, it shouldn't be a problem. So let's enter another class, ImportingHello.java, in C:\hy\tmp directory, and run the following commands to see what happens with the import statement:

C:\hy\tmp>type ImportingHello.java
/**
 * ImportingHello.java
 * Copyright (c) 2003 by Dr. Herong Yang
 */
import Hello;
public class ImportingHello {
   public static void main(String[] a) {
      System.out.println("Calling the imported Hello.main()..."); 	
      Hello.main(a);
   }
}

C:\hy\tmp>del *.class
Could Not Find C:\hy\tmp\*.class

C:\hy\tmp>del ..\src\*.class
Could Not Find C:\hy\src\*.class

C:\hy\tmp>\jdk1.3.1\bin\javac -classpath ..\src ImportingHello.java

C:\hy\tmp>\jdk1.3.1\bin\java -classpath .;..\src ImportingHello
Calling the imported Hello.main()...
Hello world!

C:\hy\tmp>del *.class

C:\hy\tmp>del ..\src\*.class

As you can see, there is no problem. The import statement makes no impact on the compilation and execution processes.

Importing Classes Defined in Unnamed Packages Using JDK 1.4.1

However, the import statement will cause compilation problem if we use JDK 1.4.1, see the following commands and outputs:

C:\hy\tmp>\j2sdk1.4.1\bin\java -version
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

C:\hy\tmp>\j2sdk1.4.1\bin\javac -classpath ..\src ImportingHello.java
ImportingHello.java:5: '.' expected
import Hello;
            ^
1 error

Obviously, specification on import statement has been changed. With JDK 1.4.1, import statement can not be used on compilation units in unnamed packages. So, from now on, stop use import statements on classes or interfaces defined in unnamed packages.

Importing Classes Defined in Unnamed Packages to Named Packages

Recently, I received an email from a reader asking me how to refer to a class defined in an unnamed package if the calling class is in a named class with JDK 1.4.1. To answer this question, let's enter a new class called "ImportingHelloCom" in a package called "com", and run the following commands:

C:\hy\tmp>type com\ImportingHelloCom.java
/**
 * ImportingHelloCom.java
 * Copyright (c) 2004 by Dr. Herong Yang
 */
package com;
import Hello;
public class ImportingHelloCom {
   public static void main(String[] a) {
      System.out.println("Calling the imported Hello.main()..."); 	
      Hello.main(a);
   }
}

C:\hy\tmp>type Hello.java
class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
   }
}

C:\hy\tmp>del *.class
Could Not Find C:\hy\tmp\*.class

C:\hy\tmp>del com\*.class
Could Not Find C:\hy\tmp\com\*.class

C:\jdk1.3.1\bin\javac com\ImportingHelloCom.java
com\ImportingHelloCom.java:5: cannot resolve symbol
symbol: class Hello
import Hello;
^
com\ImportingHelloCom.java:9: cannot resolve symbol
symbol  : variable Hello
location: class ImportingHelloCom
      Hello.main(a);
      ^
2 errors

C:\jdk1.3.1\bin\javac -classpath . com\ImportingHelloCom.java
com\ImportingHelloCom.java:6: Hello is not public in empty package; 
cannot be accessed from outside package
import Hello;
^
com\ImportingHelloCom.java:10: Hello is not public in empty package; 
cannot be accessed from outside package
      Hello.main(a);
      ^
com\ImportingHelloCom.java:10: main(java.lang.String[]) in Hello is 
not defined in a public class or interface; cannot be accessed from 
outside package
      Hello.main(a);
           ^
3 errors

This test shows us that:

  • Only public classes in an unnamed package can be imported into named packages.
  • Comparing with the testing results from the previous sections, there is really only one unnamed package. All classes defined without a package name are considered to be in this single package. All classes can be imported into each other without the "public" attribute.

(Continued on next part...)

Part:   1  2  3   4 

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Unnamed Packages