Java know how much (9) Import and Java class search path

Source: Internet
Author: User

If you want to use a class in a Java package, you must first import it using an import statement.

The import statement is somewhat similar to the #include in the C language, with the following syntax:
Import Package1[.package2 ...]. ClassName
Packages are package names, ClassName are class names. For example:
1 Import // importing the Date class under the Java.util package 2 Import // Import the Scanner class under the Java.util package 3 Import // Import all classes under the javax.swing package, * denotes all classes

Attention:
    • Imports can import only the classes that the package contains, not the packages.
    • For convenience, we generally do not import separate classes, but instead import all of the classes under the package, such as import java.util.*;.

The Java compiler defaults to all Java programs importing all the classes in the JDK's Java.lang package (import java.lang.*;), which defines some common classes, such as System, String, Object, Math, and so on. So we can use these classes directly without having to explicitly import them. However, the use of other classes must first be imported.

The "Hello World" program mentioned earlier uses SYSTEM.OUT.PRINTLN (); Statement, the System class is located in the Java.lang package, although we did not explicitly import the class in this package, but the Java compiler has imported it for us by default, or the program will fail to execute. The Java class's search path assumes the following import statement:
1 Import p1. Test;

This statement indicates that you want to import the Test class in the P1 package.

When installing the JDK, we have set the environment variable CLASSPATH to.; %java_home%\lib, and Java_home is D:\Program files\jdk1.7.0_71, so CLASSPATH is equivalent to.;D: \ Program Files\jdk1.7.0_71\lib.

The Java runtime will look in the following path and load the bytecode file Test.class:
    • . P1\test.class ("." Represents the current path)
    • D:\Program Files\jdk1.7.0_71\lib\p1\test.class

If the desired class file is found under the first path, stop the search, or continue searching for the following path, or compile or run an error if the required class file cannot be found under all paths.

You can add a search path to the CLASSPATH variable, for example.; %java_home%\lib; C:\javalib, then you can put the class file in the C:\javalib directory, the Java Runtime environment will be found.

Series Articles:

Java know how much (1) Language overview

Java know how much (2) virtual machine (JVM) and cross-platform principle

Java know how much (3) employment direction

Java know how much (4) the difference between J2SE, Java EE, J2ME

Java know how much (5) Build Java development environment

Java know how much (6) The first example of a program

Java knows how many (7) classes and objects

Java know how much (8) class library and its organizational structure

Java know how much (9) Import and Java class search path

Java know how much (9) Import and Java class search path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.