Two Types of "import" Statements

This section describes two types of 'import' statements: Single Type Import and On-Demand Type Import. 4 sample Java source files are provided to test 'import' statements.

Java offers two types of "import" statements:

1. Single Type Import: "import type_name;" - It tells JVM to load the definition of a single type immediately from the specified package.

2. On-Demand Type Import: "import package_name.*;" - It tells JVM to search the specified package for any missing type when it is needed.

"javac" command processes there two different types of "import" statements differently. I wrote the following 4 source files to test this:

1. ClsA.java:

/* ClsA.java
 * Copyright (c) 2015 HerongYang.com. All Rights Reserved.
 */
package com.herongyang.util;
public class ClsA {
   public void printInfo() {
      System.out.println("Class: com.herongyang.util.ClsA");
   }
}

2. ClsB.java:

/* ClsB.java
 * Copyright (c) 2015 HerongYang.com. All Rights Reserved.
 */
package com.herongyang.util;
public class ClsB {
   public void printInfo() {
      System.out.println("Class: com.herongyang.util.ClsB");
   }
}

3. ImportTestA.java:

/* ImportTestA.java
 * Copyright (c) 2015 HerongYang.com. All Rights Reserved.
 */
import com.herongyang.util.*;
public class ImportTestA {
   public static void main(String[] arg){
      ClsA a = new ClsA();
      a.printInfo();
      ClsB b = new ClsB();
      b.printInfo();
   }
}

4. ImportTestB.java:

/* ImportTestB.java
 * Copyright (c) 2015 HerongYang.com. All Rights Reserved.
 */
import com.herongyang.util.ClsA;
import com.herongyang.util.ClsB;
public class ImportTestB {
   public static void main(String[] arg){
      ClsA a = new ClsA();
      a.printInfo();
      ClsB b = new ClsB();
      b.printInfo();
   }
}

Read the following section to see how the "javac" compilation tool processes "import" statements.

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

javac - The Java Program Compiler

 javac - Java Compilation Command and Options

 Compiling Hello.java - My First Java Program

 "javac -classpath" - Specifying Class Path

 "javac -verbose" - Printing Compilation Details

 "javac -sourcepath" - Specifying Source Path

 "javac -d" - Specifying Output Directory

Two Types of "import" Statements

 "import" Statements Processed by "javac"

 "javac -g" - Controlling Debugging Information

 "javac --module" - Compiling Entire Module

 "javac -X" - Specifying Non-Standard Options

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB