Learning: Examples of using "package" and "import" in Java

Source: Internet
Author: User

1.package of use, solve what problem

Provides a namespace for classes, resolves naming conflicts for classes, and class file management issues

2. Examples of Use

2.1 Self-Test code

(1) The package must be the first non-comment statement of the source file

(2) A source file can only have one package

(3) No display specified is under the default package

(4) Free access to the same package

1  Packagetestpkg;2 3  Public classtestpackage{4      Public Static voidfun1 () {5System.out.println ("Msg:testpackage print");6     }7         8      Public Static voidMain (string[] args) {9 fun1 ();Ten     } One}
View Code
1 Import Testpkg.testpackage; 2 3  Public class testcallpackage{4      Public Static void Main (string[] args) {5         testpackage.fun1 ();   testPkg.testPackage.fun1 (); 6     }7 }
View Code

2.2 Execution

(1) compile with the-D as much as possible, the compiler will generate the appropriate directory structure for the Java source files

(2) Compile 1 to generate "testpkg" directory

(3) Compile 2 generate "Testcallpackage.class"

3.import of Use

(1) Do not need to add the package name when calling

(2) If two packages contain the same class name, only the package can be used. Xxx.function form, import causes compile blur error

(3) Import static: variable can be imported

4.java source file general structure diagram

Learning: Examples of using "package" and "import" in Java

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.