Importing Classes Defined in Unnamed Packages

This section provides a tutorial example on how to import classes defined in an unnamed package - done in the same as importing classed defined in named packages with JDK 1.3.1. But it is not allowed with JDK 1.4.1.

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,, in C:\hy\tmp directory, and run the following commands to see what happens with the import statement:

 * 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()..."); 	

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

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 with JDK 1.3.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 '.' 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.

Last update: 2005.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

Calling and Importing Classes Defined in Unnamed Packages

 What Is an Unnamed Package?

 One Class in an Unnamed Package -

 Two Classes in Unnamed Packages - and

Importing Classes Defined in Unnamed Packages

 Importing Classes from Unnamed to Named Packages

 Importing Classes from Unnamed to Named Packages - JDK 1.4.1

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version