Java Learning 10--package and import

Source: Internet
Author: User

Package and Import statements

To facilitate the management of a large number of classes in big software systems, solve the class naming conflict problem, the Java ingestion Package mechanism, which provides a class of multiple class namespaces, when used, import into the corresponding package packages.

Use Step one:

The package statement acts on the first statement of the Java source file, indicating the packages in which the classes defined in the file are located. (If the statement is default, it is specified as a nameless package). If you want to put a class into a package, the first sentence of the source file in this class is written in the packages statement

Format: Package xxx.xxx.xxx[.xxx ...]

Use Step two:

The Java compiler applies the package to the directory management of the file system, in the packages statement, with "." To indicate the hierarchy of packages (directories), such as using statements

1  Package // all of the class (. Class) files in the file need to be in the. \com\wzh directory. Create a new COM folder under the current directory, create a new Wzh under COM, and put the. class file in it. 

The class file (. Class) needs to be effectively placed under the relative path of the package to which it belongs. (if there is an error, it may be that the source code has an impact, you need to delete or move to another directory)

Use Step three:

If you package a class, you must use the class's full name (for example: Com.wzh.MyClass) When you use it, and the Java compiler will not find the class.

 1  //  Import Com.wzh.MyClass;  2  //  Do not use import to refer to the package  3  4  public  class   Test {  5  public  static  void   main (String args[]) { 6  com.wzh.MyClass my Class = new   Com.wzh.MyClass ();  7  }  8  } 

You can also use import to introduce the classes you want to use at the beginning of the file, for example

1 ImportCom.wzh.MyClass;//referencing the package you specify2 Importjava.util.*;//introduce all the classes in the Java.util package3 4  Public classTest {5      Public Static voidMain (String args[]) {6MyClass MyClass =NewMyClass ();7     }8}

Ps:

1, the use of classes in the same package does not need to be introduced;

2. The parent directory of the top-level package of the. class file must be located under Classpath;

3, execute a class need to write the full package name, for example: Java com.wzh.MyClass (command line)

Java Learning 10--package and import

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.