Java Tools Tutorials - Herong's Tutorial Examples - v6.24, by Herong Yang
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) 2005 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) 2005 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) 2005 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) 2005 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.
Table of Contents
►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
java - The Java Program Launcher
jpackage - Binary Package Builder
javadoc - The Java Document Generator
jdeps - The Java Class Dependency Analyzer
jdeprscan - The Java Deprecated API Scanner
jcmd - The JVM Diagnostic Tool
jconsole - Java Monitoring and Management Console
jstat - JVM Statistics Monitoring Tool
jhsdb - The Java HotSpot Debugger
jvisualvm (Java VisualVM) - JVM Visual Tool
javap - The Java Class File Disassembler
keytool - Public Key Certificate Tool
jrunscript - Script Code Shell
native2ascii - Native-to-ASCII Encoding Converter