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